| 
 | 
 
 
发表于 2022-12-3 20:11:00
|
显示全部楼层
   本楼为最佳答案    
 
 
 本帖最后由 lxping 于 2022-12-3 22:02 编辑  
 
1、因为objects是类C的父类,且object中有__getattribute__(attrname)这个魔法方法,使用super().__getattribute__(attrname)可以避免:使用return self.__getattribute__(attrname),在使用self.name或者self._age进行属性访问的时候进入的无线递归循环。 
2、删掉super(),实例化对象没有你说的情况呀,但是属性访问就会有问题 
- class C:
 
 -     def __init__(self, name, age):
 
 -         self.name = name
 
 -         self.__age = age
 
 -     def __getattribute__(self, attrname):
 
 -         print('拿来吧你')
 
 -         return __getattribute__(attrname)
 
  
- c = C("小甲鱼", 18)
 
 - c.name
 
 - 拿来吧你
 
 - Traceback (most recent call last):
 
 -   File "<pyshell#23>", line 1, in <module>
 
 -     c.name
 
 -   File "<pyshell#14>", line 7, in __getattribute__
 
 -     return __getattribute__(attrname)
 
 - NameError: name '__getattribute__' is not defined
 
  复制代码 |   
 
 
 
 |