class Test:
def __init__(self, value):
self.objattr = value
def __getattribute__(self, name):
print("__getattribute__")
return super().__getattribute__(name)
def __getattr__(self, name):
return 'No attribute'
obj = Test ("Instance Properties")
Below are the results.
__getattribute__
__getattribute__
__getattribute__
__getattribute__
__getattribute__
__getattribute__
If you create an obj instance with a Test class,
Why getattribute() is executed 6 times
Is getattribute output 6 times?
Books use point operations when creating objects.
getattribute It says to execute a print statement inside the method
I don't understand, so I'm asking you a question.
python
If you test the code on the questionnaire on the jupyter, you'll see different results from the book.
Save the code above as a py file and run it.
class Test:
def __init__(self, value):
self.objattr = value
def __getattribute__(self, name):
print(f"__getattribute__ called {name.__class__}")
breakpoint()
return super().__getattribute__(name)
def __getattr__(self, name):
return 'No attribute'
Please modify it and test it on the jupyter as above.
When the debugger prompts you for pdb, give it up, move it to the top stack, and look at the code in the list. I don't know who paged...
They'll be calling from inside ipython.
© 2024 OneMinuteCode. All rights reserved.