[Python-ideas] Decorator syntax

Georg Brandl g.brandl at gmx.net
Thu Sep 3 01:09:33 CEST 2009


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

As is this:

   @a or (c and d)
   def foo(): pass

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

However, this looks better to me:

   @(a + b)
   @(a or (c and d))

So, in terms of Grammar/Grammar, what about

   decorator: '@' atom trailer* NEWLINE


[x-post to ideas list]

Georg

-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.




More information about the Python-ideas mailing list