Python Attribute Access Order II

@(Python)

a = A()
a.x
A.x

a.x -> a.__getattribute__
it will take over and run default access order, so if overriding this method, access order would change

  1. if x is a data descriptor, call type(a)._dict_[’x’]._get_(a, type(a))
  2. if 'x' in a._dict_, get a._dict_['x']
  3. if 'x' in A._dict_ or x is a non-data descriptor, exclusive, get A._dict_['x'] or x._get_
  4. if _getattr_ is defined, it comes as a backup
  5. raise AttributeError

A.x -> A.__getattribute__

  1. x is a descriptor, call A._dict_[’x’]._get_(None, A)
  2. 'x' in A._dict_, get A._dict_['x']
  3. raise AttributeError(no _getattr_ as back up)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容