This is a special method __getattribute__ question related to Python point operators.

Asked 2 years ago, Updated 2 years ago, 12 views

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

2022-09-20 21:44

1 Answers

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.


2022-09-20 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.