|
发表于 2020-9-29 09:14:39
|
显示全部楼层
那是另一个魔法函数,第46讲不是有么?
- class CountList:
- def __init__(self,*args):
- self.values = [x for x in args]
- self.count = {}.fromkeys(range(len(self.values)),0)
- def __len__(self):
- return len(self.values)
- def __getitem__(self,key):
- print('访问我了')
- self.count[key] += 1
- return self.values[key]
- def __getattribute__(self,name):
- print('getattribute',name)
- return super().__getattribute__(name)
- print("第一句")
- c1=CountList(22,33,44,55)
- print("第二句")
- print(c1[1])
- print("第三句")
- print(c1.values[2])
复制代码 |
|