Decorating methods - where do my arguments go?

Peter Otten __peter__ at web.de
Mon May 11 12:28:54 EDT 2009


Mikael Olofsson wrote:

> Duncan Booth wrote:
> 
>> The __get__ method should be returning a new object, NOT modifying the
>> state of the decorator. As written it will break badly and unexpectedly
>> in a variety of situations:
>> 
>> [snip good examples of things going bad]
> 
> Ouch! So, does that mean that George's solution based on a function is
> the way to go, or does that mean that my __call__ should analyze the
> passed callable?

I usually use decorator functions, but I think the following should work, 
too:

class deco(object):
    def __init__(self, func):
        self._func = func
    def __call__(self, *args):
        print "Decorator:", args
        self._func(*args)
    def __get__(self, *args):
        return deco(self._func.__get__(*args))

Peter




More information about the Python-list mailing list