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)

你可能感兴趣的:(Python Attribute Access Order II)