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

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Mar 4 04:14:06 CET 2011

Raymond Hettinger wrote:

> How are tuples handled?
>    @deco
>     C = a, b, c
> Is this C('C', a, b, c) or C('C', (a, b, c))?

Good question. The former is potentially more useful,
but less obvious.

This is another thing that the 'def' version would be
better at. Because it's transforming an existing function
call rather than manufacturing a new one, you get to use
all the parameter passing machinery in an obvious way.
So you can write

   def C = deco(a, b, c)

if you want them a separate parameters.

> All in all this seems like too much firepower (a language syntax change) 
> for too little benefit (how much do we really care that 'C' got typed 
> twice?).

It actually annoys me quite a lot every time I bump into
something like this, because the language virtually forces
a DRY violation on me that's impossible, or at least
extremely awkward, to avoid.

Python is generally very good at not reserving special
powers for itself, but this is one area where there is
a mechanism (for defining a thing that knows its own name)
that works in certain specific cases (def and class) but
is not open to the programmer for easy use in a general way.
That strikes me as a wart.

> If there were going to be only one syntax change for Python 3.3,

If we only get one syntax change per major release, then
for 3.3 it's probably going to be yield-from, which Guido
recently said he would like to move forward.

> why not 
> use it for something that adds a lot more expressive power:
>     x = a!name      #  x = getattr(a, name)

That's something worth considering, but I'm not sure it's
obviously a more pressing issue.

> Another expressive bit a syntax would be for a way to do a def into a 
> target namespace.
> actions = {}
> def actions.shoot():
>        print('Fire!')
> def actions:retreat():
>        print('Run away!)

That's a worthy idea, too. Although if we're counting syntax
changes, I'd call that about 0.5 of a change, since it's really
just relaxing a restriction to allow you to write something
that it looks like you should have been able to write all

> I don't think that assignment decorators add a significant 
> new capability.  AFAICT, it just saves a few characters but doesn't 
> change the way we think or work.

As I've tried to point out, it's about more than just saving
characters. It's about allowing the programmer to follow DRY,
which is an important software engineering issue. It's also
about improving readability, because that counts, you know.


More information about the Python-ideas mailing list