The process of attribute lookup in python – Python量化投资

The process of attribute lookup in python

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.

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!