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__
and a.__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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…