Python provides us many possibilities on instance/class attribute, for example:
class A(object):
def __init__(self):
self.foo = \"hello\"
a = A(
bar = a.foo
...
a.__getattribute__('foo')
a.__dict__['foo']
foo
's .__get__()
if defined on A
.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 and instance
is an instance of Class
, evaluating instance.foobar
roughly equates to this:
Class.__getattribute__
(tp_getattro
). The default does this:
Class.__dict__
have a foobar
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 a foobar
item that is not a data descriptor [9]?
Class.__dict__['foobar'].__get__(instance, klass)
. [6]Class.__getattr__
, call Class.__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.