[python-uk] Advice on decorator grammar

Stestagg stestagg at gmail.com
Wed Apr 3 14:07:17 CEST 2013


Technically, you don't have to worry about refcounts here.

evaluating 'AClass().method' results in a 'bound' method.

The method binding contains a reference to the instance, so internally, a
reference is always held.  It does mean however that the AClass instance is
anonymous, there is no simple way to directly access it from other python
code




On Wed, Apr 3, 2013 at 12:45 PM, <a.cavallo at cavallinux.eu> wrote:

>
> 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<http://mail.python.org/mailman/listinfo/python-uk>
>>
>
> ______________________________**_________________
> python-uk mailing list
> python-uk at python.org
> http://mail.python.org/**mailman/listinfo/python-uk<http://mail.python.org/mailman/listinfo/python-uk>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-uk/attachments/20130403/c2f00dce/attachment.html>


More information about the python-uk mailing list