[Python-Dev] Decorator syntax J2 (decorate..def), with implementation (original) (raw)

Michael Sparks zathras at thwackety.com
Sat Aug 14 01:31:24 CEST 2004


Hi,

I suspect people are getting sick of the decorator discussion by now, so I'll try to be brief, and useful.

Syntax option J2 from the wiki seems to have provoked an interesting discussion[1] at least by a small subset of c.l.p, so I wondered how difficult it would be to implement - this turned out to be relatively simple [2]. [1] http://mail.python.org/pipermail/python-list/2004-August/233413.html [2] http://mail.python.org/pipermail/python-list/2004-August/233591.html (implementation)

Example of the syntax... (I'm sure it's familiar...)

#Syntax J2 from http://www.python.org/moin/PythonDecorators #Some random comment #More random comment decorate: staticmethod grammarrule('statement : expression') versioninfo("Added in 2.4") deprecated typeinfo(None) def p_statement_expr(self, p): print p[1]

For comparison, this is intended as equivalent to:

#Current syntax in 2.4a2 #Some random comment #More random comment @staticmethod @grammarrule('statement : expression') @versioninfo("Added in 2.4") @deprecated @typeinfo(None) def p_statement_expr(self, p): print p[1]

With the exact semantics and restrictions remaining the same - this is just syntactic sugar.

This was my first time looking at the python source to see how easy it would be to implement - so it's not perfect, and probably far from ideal. My reasons I looking and giving it a go though were:

I have read most of the arguments against this syntax, and if the decision ends up to stay with @ or take a different syntax, I'm sure it'll work out fine :) (This isn't my favourite syntax, my favourite has a keyword inside the function suite, which I'm well aware is dead in the water!)

The post I made to c.l.p with a first attempt at an implementation (has minor issues regarding scopes, but does work) can be found here:

If there is interest in this syntax, then I suspect/hope making it suitable for a "proper" patch would be relatively simple.

In case people are wondering what the grammar change I made for this is, I'll include here:

-decorator: '@' dotted_name [ '(' [arglist] ')' ] +decorator: dotted_name [ '(' [arglist] ')' ] decorators: decorator ([NEWLINE] decorator)* NEWLINE -funcdef: [decorators] 'def' NAME parameters ':' suite +funcdef: ['decorate' ':' NEWLINE INDENT decorators DEDENT ] 'def' NAME parameters ':' suite parameters: '(' [varargslist] ')'

Downsides here:

Biggest upside from a personal perspective though is this: (aside from others)

Example tested:

class Foo: staticmethod # This is cruft to force staticmethod into scope

decorate: staticmethod def hello(who): print "woo?", who

Anyway, hope this is considered useful, rather than extending the headache! (However, I am for one very happy that decorators are now available in something clearer than we had in pre(2.4) and it certainly made implementing the syntax/semantics above relatively trivial :)

Regards,

Michael.

"The 'nice' thing about decorator syntaxes is there are so many to choose from."



More information about the Python-Dev mailing list