[python-uk] Advice on decorator grammar

a.cavallo at cavallinux.eu a.cavallo at cavallinux.eu
Wed Apr 3 13:45:07 CEST 2013


My first tought would be the mydecorator = MyDecorator() will hold the 
object instance reference and the ref count won't go to zero..




> So..
>
> 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
>     @MyDecorator().decorator_method()
>                   ^
> 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__.
>
> S
> _______________________________________________
> python-uk mailing list
> python-uk at python.org
> http://mail.python.org/mailman/listinfo/python-uk



More information about the python-uk mailing list