On Wed, Oct 19, 2011 at 3:43 PM, Chris Rebert firstname.lastname@example.org wrote:
On Wed, Oct 19, 2011 at 2:32 PM, Tarek Ziadé email@example.com wrote: Hello
Today I've tried to write a one-liner for a decorator, The decorator is a method in a class.
I wanted to do something like this:
@Class().decorator() def function(): ...
That threw a syntax error to my surprise.
<snip> > Is there something obvious I am missing, or is there a weird thing in > the way decoratirs are parsed ?
PEP 318 -- Decorators for Functions and Methods (http://www.python.org/dev/peps/pep-0318/ ): "Current Syntax [...] The decorator statement is limited in what it can accept -- arbitrary expressions will not work. Guido preferred this because of a gut feeling ." : http://mail.python.org/pipermail/python-dev/2004-August/046711.html
According to Python 2.7's grammar (http://docs.python.org/reference/grammar.html ): decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE dotted_name: NAME ('.' NAME)*
So, you're limited to an arbitrarily-long sequence of attribute accesses, followed by an optional call.
Interesting. I would have expected at least the following to work:
decorator: '@' NAME trailer* NEWLINE trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Regardless, good to know, even if uncommon.
Cheers, Chris -- http://rebertia.com _______________________________________________ Python-ideas mailing list Pythonfirstname.lastname@example.org http://mail.python.org/mailman/listinfo/python-ideas