Descriptor HowTo Guide



  • __get__()
  • __set__()
  • __delete__()

data descriptor

  • 对象:object.getattribute():transforms b.x into type(b).dict['x'].get(b, type(b))
  • 类:type.getattribute():transforms B.x into B.dict['x'].get(None, B).

The important points to remember are:

  • descriptors are invoked by the __getattribute__() method
  • overriding __getattribute__() prevents automatic descriptor calls
  • __getattribute__() is only available with new style classes and objects
  • object.__getattribute__() and type.__getattribute__() make different calls to __get__().`
  • data descriptors always override instance dictionaries.
  • non-data descriptors may be overridden by instance dictionaries.


unbound method&bound method

Likewise, the effects of calling a method object depend on the im_self field. If set (meaning bound), the original function (stored in the im_func field) is called as expected with the first argument set to the instance. If unbound, all of the arguments are passed unchanged to the original function. The actual C implementation of instancemethod_call() is only slightly more complex in that it includes some type checking.

Static Methods and Class Methods


The non-data descriptor transforms an obj.f(*args) call into f(obj, *args). Calling klass.f(*args) becomes f(*args).

comments powered by Disqus