On Difference between __getattr__ vs __getattribute__ there are some nice examples to __getattr__
and _getattribute__
.
Why is __getattribute__
called 19 times - after the code?
Ok - it's a recursion ... but why?
class Count(object):
def __init__(self,mymin,mymax):
self.mymin=mymin
self.mymax=mymax
self.current=None
def __getattr__(self, item):
self.__dict__[item]=0
return 0
def __getattribute__(self, item):
print("__getattribute__: ", item) # only this line is new!!!!
if item.startswith('cur'):
raise AttributeError
return object.__getattribute__(self,item)
# or you can use ---return super().__getattribute__(item)
# note this class subclass object
obj1 = Count(1,10)
print(obj1.mymin)
print(obj1.mymax)
print(obj1.current)
print("end")
Output:
__getattribute__: mymin
1
__getattribute__: mymax
10
__getattribute__: current
__getattribute__: __dict__
0
end
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__
__getattribute__: __class__