[python-uk] Advice on decorator grammar

Simon Yarde simonyarde at me.com
Wed Apr 3 13:34:47 CEST 2013

Hi All

I've not posted to this list before. Hello!

I have a question about decorators and have failed to devise a search that has thrown up any history of discussion on this particular matter.

Does the following seem like something that 'should' work? Or is anyone aware of a source of documentation that explains historically why the following syntax might not be allowed?

I hope this sort of conundrum/discussion-point is appropriate to this forum; I'm not on python-dev and this is obviously not a bug.


Decorator grammar is this:

decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE

The grammar prevents this:

>>> class MyDecorator:
...     def decorator_method():
...             pass
>>> @MyDecorator().decorator_method()
  File "<stdin>", line 1
SyntaxError: invalid syntax

But is possible to achieve the desired effect by assigning the class instance to variable:

>>> mydecorator = MyDecorator()
... @mydecorator.decorator_method
... def f():

My initial thoughts were that the syntax provided a neat way to provide a configurable decorator class instance with a number of alternative decorator-function generating methods, rather than just the usual __call__.


More information about the python-uk mailing list