@decorator syntax is sugar, but for what exactly?

xtian xtian at toysinabag.com
Mon Aug 9 18:06:50 EDT 2004


Avner Ben <avner at skilldesign.com> wrote in message news:<4117a43b$1 at news.012.net.il>...
> xtian wrote:
> >[snip]
> > 
> > def property_(f):
> >     return property(*f())
> 
> Where has the property Without Underscore come from?
> 

property has been a builtin since 2.2, along with classmethod and
staticmethod - it's part of the new-style classes guff.

See: http://www.python.org/2.2/descrintro.html#property
(You'll probably want to read the whole thing.)


> >[snip]
> >     
> > t = DecorationTest()
> > print t.foo
> > t.foo = 3
> > print t.foo
> > 
> 
> Will you please explain how the assignment and retrieval work?
> 
> 	Avner.

The key to it is that the property call makes foo a descriptor, with
__get__ and __set__ methods that use the getter and setter we passed
in. That's what triggers the attribute-getting and -setting methods.

The descriptor API is a big part of the type/class unification. here's
some more information: http://www.python.org/peps/pep-0252.html
(I haven't actually finished reading it myself - if my explanation's a
bit garbled, ignore it and read the link. :)

Hope that helps!
xtian



More information about the Python-list mailing list