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
:
- direct access
a.foo
- inner dict
a.__dict__['foo']
- get and set
a.__get__
anda.__set__
,of course there two are pre-defined methods. - getattribute
a.__getattribute__
__getattr__
and__setattr__
- 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?
bar = a.foo
...a.__getattribute__('foo')
a.__dict__['foo']
foo
's.__get__()
if defined onA
.The returned value would then be assigned to
bar
.a.foo = bar
...a.__getattribute__('foo')
a.__dict__['foo']
foo
's.__set__(bar)
if defined on A.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 andinstance
is an instance ofClass
, evaluatinginstance.foobar
roughly equates to this:Class.__getattribute__
(tp_getattro
). The default does this:Class.__dict__
have afoobar
item that is a data descriptor ?Class.__dict__['foobar'].__get__(instance, Class)
.instance.__dict__
have a'foobar'
item in it?instance.__dict__['foobar']
.Class.__dict__
have afoobar
item that is not a data descriptor [9]?Class.__dict__['foobar'].__get__(instance, klass)
. [6]Class.__getattr__
, callClass.__getattr__('foobar')
.There is an illustrated image for this:
Please do check out the original blog if interested which gives a outstanding explanation on python class, attribute lookup, and metaclass.