Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Decorator and Metaclasses Documentation

1 view
Skip to first unread message

sysfault

unread,
Aug 21, 2005, 7:29:17 PM8/21/05
to
Does anyone know of any good documentation on these topics, doesn't look
like the official python tutorial covers them. Thanks in advance.
--
A wise man knows he knows nothing.

Jeffrey E. Forcier

unread,
Aug 21, 2005, 8:01:13 PM8/21/05
to
Amusingly, I was just perusing these links earlier today. Go go Firefox
history search!

http://www.python.org/2.2/descrintro.html
http://users.rcn.com/python/download/Descriptor.htm

sysfault

unread,
Aug 21, 2005, 8:36:04 PM8/21/05
to


Are descriptors the same thing as decorators?

Mike C. Fletcher

unread,
Aug 21, 2005, 9:36:29 PM8/21/05
to sysfault, pytho...@python.org
sysfault wrote:

>Does anyone know of any good documentation on these topics, doesn't look
>like the official python tutorial covers them. Thanks in advance.
>
>

PyCon 2005, PyCon 2004 and PyGTA November 2004 presentations here are
all on these topics:
http://www.vrplumber.com/programming/

Though the don't go into extreme detail on decorators (they are
basically syntactic sugar for a particular type of descriptor).

HTH,
Mike

--
________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Steve Holden

unread,
Aug 21, 2005, 9:44:48 PM8/21/05
to pytho...@python.org
sysfault wrote:
> On Sun, 21 Aug 2005 17:01:13 -0700, Jeffrey E. Forcier wrote:
>
>
>>Amusingly, I was just perusing these links earlier today. Go go Firefox
>>history search!
>>
>>http://www.python.org/2.2/descrintro.html
>>http://users.rcn.com/python/download/Descriptor.htm
>
>
>
> Are descriptors the same thing as decorators?

No. In brief:

Decorators are a mechanism whereby a function or method can be
transparently wrapped by another function.

Descriptors are used to hook programmed functionality into the basic
access mechanisms of object attributes.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Jeffrey E. Forcier

unread,
Aug 21, 2005, 11:00:40 PM8/21/05
to
Sorry about that, I misread the original question. However, the
python.org link is still valid as it concerns metaclasses as well as a
handful of other topics.

Michele Simionato

unread,
Aug 22, 2005, 2:42:49 AM8/22/05
to
There are also my lectures at Oxford:

http://www.reportlab.org/~andy/accu2005/pyuk2005_simionato_wondersofpython.zip

Michele Simionato

bruno modulix

unread,
Aug 22, 2005, 3:37:55 AM8/22/05
to
Mike C. Fletcher wrote:
(snip)

> Though the don't go into extreme detail on decorators (they are
> basically syntactic sugar for a particular type of descriptor).
>
Err... Could you elaborate on this ? Decorators are syntactic sugar for
function wrapping, while descriptors are a 'protocol' to hook into
attribute lookup, so I don't really see how one can describe the first
in terms of the second...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'on...@xiludom.gro'.split('@')])"

Mike C. Fletcher

unread,
Aug 22, 2005, 11:57:31 AM8/22/05
to bruno modulix, pytho...@python.org
bruno modulix wrote:

>Mike C. Fletcher wrote:
>(snip)
>
>
>>Though the don't go into extreme detail on decorators (they are
>>basically syntactic sugar for a particular type of descriptor).
>>
>>
>>
>Err... Could you elaborate on this ? Decorators are syntactic sugar for
>function wrapping, while descriptors are a 'protocol' to hook into
>attribute lookup, so I don't really see how one can describe the first
>in terms of the second...
>
>

There are two major types of descriptors, the elven and the dwarven.

* Elven descriptors are things like property, BasicProperty, VRML97
fields, Zope Field Properties, or PEAK's mechanisms. They mediate
access to an instance's attributes, (both setting and getting of
values) and are used primarily for domain modeling.
* Dwarven descriptors are things like staticmethod or classmethod;
function-like things that tend to look like a function and quack
like a function, but have some special property or functionality
attached when accessed as an attribute of a class/instance
(functions themselves return instance methods or class methods
depending on how they are retrieved).

As you'll read in the PyCon 2005 paper pointed to, the whole set
basically grew out of a generalisation of what functions were already
doing (conceptually). Functions are, in short, the proto-descriptor.
That is, objects which control what result is returned by attribute
access (e.g. an instance or unbound instance method). Many (most?)
dwarven descriptors are implemented as simple wrapper functions around
the base function to use the function's already-present descriptor hooks.

Decorators are syntactic sugar for defining dwarven descriptors. In
Python 2.2 and 2.3 a dwarven descriptor was instantiated like this:

def function( cls ):
"""Do something simple with the class"""
function = classmethod( function )

in Python 2.4+, they can be instantiated like this:

@classmethod
def function( cls ):
"""Do something simple with the class"""

technically you *could* return something that's not a descriptor from
the decorator (e.g. classmethod), but it's unlikely you'd do that very
often.

Bruno Desthuilliers

unread,
Aug 22, 2005, 3:51:16 PM8/22/05
to
Mike C. Fletcher a écrit :

> bruno modulix wrote:
>
>> Mike C. Fletcher wrote:
>> (snip)
>>
>>
>>> Though the don't go into extreme detail on decorators (they are
>>> basically syntactic sugar for a particular type of descriptor).
>>>
>>>
>>
>> Err... Could you elaborate on this ? Decorators are syntactic sugar for
>> function wrapping, while descriptors are a 'protocol' to hook into
>> attribute lookup, so I don't really see how one can describe the first
>> in terms of the second...
>>
>>
> There are two major types of descriptors, the elven and the dwarven.
>
> * Elven descriptors are things like property, BasicProperty, VRML97
> fields, Zope Field Properties, or PEAK's mechanisms. They mediate
> access to an instance's attributes, (both setting and getting of
> values) and are used primarily for domain modeling.
> * Dwarven descriptors are things like staticmethod or classmethod;
> function-like things that tend to look like a function and quack
> like a function, but have some special property or functionality
> attached when accessed as an attribute of a class/instance
> (functions themselves return instance methods or class methods
> depending on how they are retrieved).
>

Ok, I see...

In fact, while reading your explanations, it reminded me of some
experiments I made recently playing with descriptors, decorators and
metaclasses to use callable objects implementing the descriptor protocol
as instance methods of other objects (I don't know if I'll ever find a
use case for this trick, but what, they're a lot of things in Python I
never thought I could find a use case for at first and that I'd have
hard time living without nowadays...).

>
> HTH,

It did, thanks.

Chris Smith

unread,
Aug 24, 2005, 1:33:14 PM8/24/05
to
>>>>> "Michele" == Michele Simionato <michele....@gmail.com> writes:

Michele> There are also my lectures at Oxford:
Michele> http://www.reportlab.org/~andy/accu2005/pyuk2005_simionato_wondersofpython.zip

Michele> Michele Simionato

You really need to get O'Reilly to publish your stuff.
-Chris

0 new messages