class List(list): #继承list所有的属性,也可以派生出自己新的,比如append和mid
def append(self, p_object):
' 派生自己的append:加上类型检查'
if not isinstance(p_object,int):
raise TypeError('must be int') #添加报错信息提示用户'must be int'
super().append(p_object)
@property
def mid(self): #给列表添加一个返回列表中间元素的功能
'新增自己的属性'
index=len(self)//2
return self[index]
l=List([1,2,3,4])
print(l) #[1,2,3,4]
l.append(5)
# l.append('6') #报错,必须为int类型
print(l) #[1,2,3,4,5]
print(l.mid) # 3
#其余的方法都继承list的
l.insert(0,0)
print(l) #[0,1,2,3,4,5]
l.clear()
print(l) #[]