2.2 descriptor API

Guido van Rossum guido at python.org
Fri Apr 5 14:00:21 EST 2002


Daniel Popowich wrote:
> 1.  Why is the __get__ method called on a descriptor when it is
>     referenced as a class attribute?

This was a requirement.  When you get a method out of a class in Python 2.1
and before, it gets special treatment which gives you an "unbound method".
The unbound method insists that you call it with an instance of the right
class, in order to prevent mistakes.  The new descriptor API must be able
to support similar behavior.  But it must also support other behavior, like
class methods and static methods.

> 2.  What is the purpose behind the second argument; does someone have
>     an example of how or when I would make use of it?

When __get__ is called on a class, the first argument is None, and the
second argument is the class.

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



More information about the Python-list mailing list