Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
565 views
in Technique[技术] by (71.8m points)

methods - How can I know where an attribute is called? Python

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Special Method Names in the docs lists all of the methods that get called implicitly by the language syntax.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...