[Python-ideas] Assignment decorators (Re: The Descriptor Protocol...)

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Mar 4 01:19:19 CET 2011


Ethan Furman wrote:

> Ah -- I thought 'decorator' was a stand-in for an actual decorator name.

Yes, it is -- but it's a name that you would otherwise have to
write somewhere else. For example, currently you write

   Fred = namedtuple('Fred', 'x y z')

This would become

   @namedtuple
   Fred = 'x y z'

> Didn't someone say that meta-classes 
> can be used to solve this issue?

Probably they can, but at the expense of abusing 'class' to define
something that in general is nothing like a class.

For example, in PyGUI I make heavy use of a custom property
descriptor which is currently used like this:

   fred = overridable_property('fred', "This is the fred property.")

Using an assignment decorator, this would become

   @overridable_property
   fred = "This is the fred property."

There is probably some way of arranging thing so that it can be
written

   class fred(overridable_property):
     "This is the fred property."

but this would be massively confusing, because it's not defining
a class at all, or anything remotely like a class.

What we really want is a construct that works like 'class'
in some ways, but has a completely neutral name that doesn't
suggest any particular semantics.

The word 'def' would actually fill that bill if it weren't
already ingrained in everyone's brains that it defines functions
in particular.

-- 
Greg



More information about the Python-ideas mailing list