[Python-ideas] Decorator syntax

MRAB python at mrabarnett.plus.com
Wed Sep 2 23:42:33 CEST 2009

Georg Brandl wrote:
> MRAB schrieb:
>> James Y Knight wrote:
>>> On Sep 2, 2009, at 6:15 AM, Rob Cliffe wrote:
>>>> So - the syntax restriction seems not only inconsistent, but 
>>>> pointless; it doesn't forbid anything, but merely means we have to do 
>>>> it in a slightly convoluted (unPythonesque) way.  So please, Guido, 
>>>> will you reconsider?
>>> Indeed, it's a silly inconsistent restriction. When it was first added I 
>>> too suggested that any expression be allowed after the @, rather than 
>>> having a uniquely special restricted syntax. I argued from consistency 
>>> of grammar standpoint. But Guido was not persuaded. Good luck to you. :)
>> [snip]
>> I can see no syntactic reason to restrict what can appear after the @.
>> If someone chooses to abuse it then that's unPythonic, but not illegal.
> I do see a reason.  I have no problems with
>    @foo.bar
>    @foo.bar[baz]
>    @foo.bar(baz)
> But this is ugly to me:
>    @a + b
>    def foo(): pass
Ugly, yes.

> As is this:
>    @a or (c and d)
>    def foo(): pass

> Having the decorator expression "opened" by @ but not "closed" feels bad.


isn't "closed" either.

> However, this looks better to me:
>    @(a + b)
>    @(a or (c and d))
Conditions in 'if' and 'while' statements don't need parentheses, so why
do decorators?

> So, in terms of Grammar/Grammar, what about
>    decorator: '@' atom trailer* NEWLINE
I say "keep it clean", ie no parentheses except where operator priority 
or clarity require them.

IMHO, if a user writes something that's ugly then call it unPythonic;
consenting adults and all that. :-)

More information about the Python-ideas mailing list