需求:app
基於受權定製本身的列表類型,要求定製的本身的__init__方法,
定製本身的append:只能向列表加入字符串類型的值
定製顯示列表中間那個值的屬性(提示:property)
其他方法都使用list默認的(提示:__getattr__加反射)spa
1 class List: 2 def __init__(self,value): 3 self.x=list(value) 4 def append(self,value): 5 if not isinstance(value,str): 6 raise TypeError('append到列表的內的元素必須是str類型') 7 self.x.append(value) 8 def insert(self,index,value): 9 self.x.insert(index,value) 10 def __getattr__(self, item): 11 return getattr(self.x,item) 12 13 @property 14 def type(self): 15 print(self.x) 16 t=int(len(self.x)/2) 17 print(self.x[t],type(self.x[t])) 18 19 20 l=List([1,2,3]) 21 l.append("egon") 22 l.append('hello') 23 l.append('alex') 24 l.insert(7,5) 25 l.pop() 26 l.type