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
482 views
in Technique[技术] by (71.8m points)

class - What is the python attribute get and set order?

Python provides us many possibilities on instance/class attribute, for example:

class A(object):
    def __init__(self):
        self.foo = "hello"

a = A()

There are many ways to access/change the value of self.foo:

  1. direct access a.foo
  2. inner dict a.__dict__['foo']
  3. get and set a.__get__ and a.__set__,of course there two are pre-defined methods.
  4. getattribute a.__getattribute__
  5. __getattr__ and __setattr__
  6. maybe more.

While reading source code, I always get lost of what's their ultimate access order? When I use a.foo, how do I know which method/attribute will get called actually?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

I found out this great post that has a detailed explanation on object/class attribute lookup.

For object attribute lookup:

Assuming Class is the class and instance is an instance of Class, evaluating instance.foobar roughly equates to this:

  • Call the type slot for Class.__getattribute__ (tp_getattro). The default does this:
    • Does Class.__dict__ have a foobar item that is a data descriptor ?
      • If yes, return the result of Class.__dict__['foobar'].__get__(instance, Class).
    • Does instance.__dict__ have a 'foobar' item in it?
      • If yes, return instance.__dict__['foobar'].
    • Does Class.__dict__ have a foobar item that is not a data descriptor [9]?
      • If yes, return the result of Class.__dict__['foobar'].__get__(instance, klass). [6]
  • If the attribute still wasn't found, and there's a Class.__getattr__, call Class.__getattr__('foobar').

There is an illustrated image for this:

enter image description here

Please do check out the original blog if interested which gives a outstanding explanation on python class, attribute lookup, and metaclass.


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

...