The process of attribute lookup in python

Python publisher01 37℃

1) If attrname is a special (i.e. Python-provided) attribute for objectname, return it.
(The attribute of Python-provided are something like ‘this’:
__lolcat__, __doc__, __weakref__ and so on)
2)Check objectname.__class__.__dict__ for attrname.
If it exists and is a data-descriptor, return the descriptor result. Search all bases of objectname.__class__ for the same case.
3)Check objectname.__dict__ for attrname, and return if found.
If objectname is a class, search its bases too.
If it is a class and a descriptor exists in it or its bases,
return the descriptor result.
4)Check objectname.__class__.__dict__ for attrname.
If it exists and is a non-data descriptor, return the descriptor result.
If it exists, and is not a descriptor, just return it.
(If it exists and is a data descriptor, we shouldn’t be here because we would have returned at point 2.)
Search all bases of objectname.__class__ for same case.
5)Raise AttributeError.

转载请注明:Python量化投资 » The process of attribute lookup in python

喜欢 (0)or分享 (0)