[Python-Dev] Customizing the binding of attributes

Guido van Rossum guido@python.org
Thu, 23 Aug 2001 15:23:42 -0400


> In Python 2.2, is it possible to customize the binding
> of class attributes, to emulate the normal behaviour
> of methods (function -> unbound method -> bound method)?
> 
> I think I have read something about this, but I can't
> find it anymore.

I'm not sure what you are asking about, but let me give an example.
You can write an auxiliary class that "describes" an instance
attribute.  Instances of this "descriptor" class are stored in a class
dict.  The descriptor class implements a method __get__ which is
called in two situations:

(a) when a class attribute is retrieved, <attr>.__get__(None, <class>)
    is called; this can return an unbound method

(b) when an instance attribute is retrieved, <attr>.__get__(<inst>, <class)
    is called; this can return a bound method

Does this help?

--Guido van Rossum (home page: http://www.python.org/~guido/)