Guido van Rossum wrote:
Yes, please. Here's a quick explanation of descriptors:
A descriptor is something that lives in a class' __dict__, and primarily affects instance attribute lookup. A descriptor has a __get__ method (in C this is the tp_descrget function in its type object) and the instance attribute lookup calls this to "bind" the descriptor to a specific instance. This is what turns a function into a bound method object in Python 2.2. In earlier versions, functions were special-cased by the instance getattr code; the special case has been subsumed by looking for a __get__ method. Yes, this means that a plain Python function object is a descriptor! Because the instance getattr code returns whatever __get__ returns as the result of the attribute lookup, this is also how properties work: they have a __get__ method that calls the property-get" function.
A descriptor's __get__ method is also called for class attribute lookup (with the instance argument set to NULL or None). And a descsriptor's __set__ method is called for instance attribute assignment; but not for class attribute assignment.
Hope this helps!
Could you put such short overviews somewhere on the Python Wiki ? They sure help in understanding what is going on behind the scenes without having to grep through tons of source code :-) Thanks, -- Marc-Andre Lemburg eGenix.com Professional Python Software directly from the Source (#1, Apr 17 2003)
Python/Zope Products & Consulting ... http://www.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
EuroPython 2003, Charleroi, Belgium: 68 days left