[Python-ideas] The way decorators are parsng
Tarek Ziadé
ziade.tarek at gmail.com
Wed Oct 19 23:32:21 CEST 2011
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.
But the semantic is correct, since I am currently writing:
obj = Class()
@obj.decorator()
def function():
...
And I can also write
dec = Class().decorator
@dec()
def function():
...
Is there something obvious I am missing, or is there a weird thing in
the way decoratirs are parsed ?
Demo:
>>> class Some(object):
... def stuff(self, func):
... return func
...
>>> s = Some()
>>> @s.stuff
... def ok():
... print 'ok'
...
>>> ok()
ok
>>> s = Some().stuff
>>> @s
... def ok():
... print 'ok'
...
>>> ok()
ok
>>> @Some().stuff
File "<stdin>", line 1
@Some().stuff
^
SyntaxError: invalid syntax
Cheers
Tarek
--
Tarek Ziadé | http://ziade.org
More information about the Python-ideas
mailing list