http://www.python.org/2.2/descrintro.html
http://users.rcn.com/python/download/Descriptor.htm
Are descriptors the same thing as decorators?
>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
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/
http://www.reportlab.org/~andy/accu2005/pyuk2005_simionato_wondersofpython.zip
Michele Simionato
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'on...@xiludom.gro'.split('@')])"
>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.
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.
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