We can call an attribute directly through object.attribute
, but I guess there are cases that they are called implicit. The code below shows what i believe it's happening:
class Test():
list=[0,1]
def __init__(self,age):
print("before __getattribute__")
self.list[0]=1
print("after __getattribute__")
def __getattribute__(self,attribute):
print("Initializing getattribute")
return object.__getattribute__(self,attribute)
def __setattr__(self,attribute,value):
print("Calling setattr")
test=Test(4)
The output is:
before __getattribute__
Initializing getattribute
after __getattribute__
So self.list[0]=1
is calling an attribute, since its invoking the __getatrtibute__
.
What are other cases that this may happen?
question from:
https://stackoverflow.com/questions/66066791/how-can-i-know-where-an-attribute-is-called-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…