我有这个课:
class Meta:
def __getattr__(self, atr):
print('get', atr)
def __setattr__(self, atr, atr_value):
print('{0} with value {1} added'.format(atr, atr_value))
self.__dict__[atr] = atr_value
当我设置属性时-
print
起作用:>>> x.a = 1
a with value 1 added
但是当我称它为:
>>> x.a
1
返回的值不带
print
输出。帮助将不胜感激。
最佳答案
仅当属性不存在时才调用__getattr__
。您要覆盖__gettattribute__
。
def __getattribute__(self, name):
print("get", name)
return object.__getattribute__(self, name)
关于python - 打印在__getattr__中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30874966/