2.2 descriptor API

Guido van Rossum guido at python.org
Wed Apr 10 20:54:50 EDT 2002


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

[Guido]
> > This was a requirement.[...]

[Daniel again]
> My questions had more to do with design than technical facts.  It
> strikes me that the __get__ method is not so much the publication of a
> new API, but rather the exposure of low-level, run-time interpreter
> mechanics.

Sure, but it's a very useful piece of mechanics.  E.g. in Zope 3,
new descriptor classes implementing custom __get__ methods are
under development that do things that Zope's master architect likes
to do.

It's no different than __getattr__ or __add__: it allows you to
write a class that does certain kinds of useful magic just like
Python's built-in classes do.

> But I question the utility of this for Joe Schmoe, python programmer.

Of course, if you don't need it, don't use it.  That's no reason
not to have it available in the language.

> If instead of using the descriptor api, the language had constructs
> like the following to define methods:
> 
>     class Foo:
>        # static method definition
>        def static foo(*args): pass
> 
>        # class method definition
>        def class bar(cls, *args): pass
> 
>        # instance method definition
>        def baz(self, *args): pass

When I added static and class methods and properties to Python 2.2,
I promised myself (and others) that I would eventually make up special
syntax.  But in the mean time, I implemented the underlying mechanism
and used it to provide provisional ways to spell the new features.

(The mechanism was easily decided on.  The new syntactic sugar
has to be carefully considered.)

> in other words, after "def", allow a qualifier (or perhaps a list of
> qualifiers) before the function name, then the interpreter can mark
> the functions appropriately and do the right magic when the
> class/instance attribute is accessed (*).

This and many different proposals have been discussed before.
Unfortunately I don't have the reference at hand; maybe some
other reader can help you out.

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



More information about the Python-list mailing list