[Python-Dev] PEP 318: Decorators last before colon

Phillip J. Eby pje at telecommunity.com
Tue Mar 30 17:19:44 EST 2004


At 01:21 PM 3/30/04 -0800, Guido van Rossum wrote:
> > Another possibility that has been suggested is
> >
> > [decorator]
> > def func(arg, arg):
>
>And one that I currently favor.  I'm out of bandwidth to participate
>on a msg-by-msg basis, but perhaps folks can see if they can come to
>terms with this solution?
>
>--Guido van Rossum (home page: http://www.python.org/~guido/)

I showed this to a Python programmer at my office.  He immediately asked if 
this:

if foo:
    [decorator1]
else:
    [decorator2]
def func(arg,arg):
    ...

was valid.  He then further commented that it was strange to have a Python 
construct split across two lines, and inquired whether these variants:

[decorator] def func(arg,arg):
     ...

[decorator1, decorator2
] def func(arg,arg):

[decorator] \
def func(arg,arg):
     ...

would be considered legal as well.

I also showed him the implemented syntax of 'def func(args) [decorators]:', 
and he thought it seemed natural and Pythonic, and he suggested 'as' as 
another possible alternative.  We also discussed the issue of evaluation 
order, and wondered if perhaps the decorator order in your latest syntax 
should be in reverse of the order used for decorators-at-the-end.  His last 
comment was that he thought it seemed very Perlish to begin a Python 
statement with special symbols, that then modify the behavior of code on a 
subsequent line.

Some of his questions also got me to wondering how the grammar would even 
handle the initial proposal, since it seems it would have to be phrased as 
"lists can have an optional 'NEWLINE function-definition' clause after them".

Anyway, I think I could manage to live with this syntax, although I really 
don't like it either, for many of the same reasons.  I don't think that 
putting decorators at the end of the definition impedes visibility unless 
there are a really large number of arguments or a large amount of decorator 
data.  In both cases, you're going to need to be reading more carefully 
anyway.  For the common cases, it's all going to be on one line anyway.

Oh...  one more thing...  the new proposal brings back up (to a limited 
extent) the "what to look up" question, in a different form.  More 
precisely, someone who has learned basic Python syntax may wonder why 
somebody is creating a list with 'classmethod' in it, but unless they 
already know about the syntax rule, they have no way to even *guess* that 
it has something to do with the function definition that follows.  By 
contrast, the decorators-last syntax is visibly part of the 'def' statement 
and gives much more clue as to its purpose.




More information about the Python-Dev mailing list