class Test():
def __init__(self,age):
self.age=age
def __getattribute__(self,attribute):
print("Initializing getattribute")
return 6
def __setattr__(self,attribute,value):
print("Initializing setattr")
return object.__setattr__(self,attribute,value)
test=Test(4)
test.age
print(test.age)
From the code above the result is :
Initializing setattr
Initializing getattribute
Initializing getattribute
6
I understand where each dunder method is called, but what do they really do?In the previous example getattribute dictate the attribute value and if I delete the line :
return object.__setattr__(self,attribute,value)
Nothing changes.
So what does __setattr__
do?