Re: Decorating methods - where do my arguments go?

2009-05-12 Thread Mikael Olofsson
Peter Otten wrote: 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):

Re: Decorating methods - where do my arguments go?

2009-05-12 Thread Michele Simionato
On May 12, 8:54 am, Mikael Olofsson mik...@isy.liu.se wrote: Peter Otten wrote: 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

Re: Decorating methods - where do my arguments go?

2009-05-12 Thread Duncan Booth
Michele Simionato michele.simion...@gmail.com wrote: On May 12, 8:54 am, Mikael Olofsson mik...@isy.liu.se wrote: Peter Otten wrote: I usually use decorator functions, but I think the following should wor k, too: class deco(object):     def __init__(self, func):        

Re: Decorating methods - where do my arguments go?

2009-05-12 Thread Mikael Olofsson
Michele Simionato wrote: Still it turns something which is a function into an object and you lose the docstring and the signature. pydoc will not be too happy with this approach. Duncan Booth wrote: I don't know why Mikael wants to use a class rather than a function but if he wants to save

Re: Decorating methods - where do my arguments go?

2009-05-11 Thread Michele Simionato
On May 8, 5:33 pm, Mikael Olofsson mik...@isy.liu.se wrote:   class test_decorator(object): ...     def __init__(self,func): ...         self._func = func ...     def __call__(self, *args): ...         print 'Decorator:', args ...         self._func(*args) Or you could use the decorator

Re: Decorating methods - where do my arguments go?

2009-05-11 Thread Mikael Olofsson
Peter Otten wrote: You have to turn your decorator into a descriptor by providing a __get__() method. A primitive example: class test_decorator(object): def __init__(self,func): self._func = func def __call__(self, *args): print 'Decorator:', args

Re: Decorating methods - where do my arguments go?

2009-05-11 Thread Mikael Olofsson
George Sakkis wrote: Yes, just return an actual function from the decorator instead of a callable object: def test_decorator2(func): def wrapper(*args): print 'Decorator2:', args func(*args) return wrapper class cls(object): @test_decorator def

Re: Decorating methods - where do my arguments go?

2009-05-11 Thread Duncan Booth
Mikael Olofsson mik...@isy.liu.se wrote: George Sakkis decorator function solution seems to work equally well for functions and methods. However, I prefer the cleaner encapsulation given by a class. Based on those observations, I think I will use the following approach: class

Re: Decorating methods - where do my arguments go?

2009-05-11 Thread Mikael Olofsson
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

Re: Decorating methods - where do my arguments go?

2009-05-11 Thread Peter Otten
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

Re: Decorating methods - where do my arguments go?

2009-05-09 Thread George Sakkis
On May 8, 11:33 am, Mikael Olofsson mik...@isy.liu.se wrote: Hi all! I have long tried to avoid decorators, but now I find myself in a situation where I think they can help. I seem to be able to decorate functions, but I fail miserably when trying to decorate methods. The information I have

Re: Decorating methods - where do my arguments go?

2009-05-09 Thread MRAB
Mikael Olofsson wrote: Hi all! I have long tried to avoid decorators, but now I find myself in a situation where I think they can help. I seem to be able to decorate functions, but I fail miserably when trying to decorate methods. The information I have been able to find on-line focuses on

Decorating methods - where do my arguments go?

2009-05-08 Thread Mikael Olofsson
Hi all! I have long tried to avoid decorators, but now I find myself in a situation where I think they can help. I seem to be able to decorate functions, but I fail miserably when trying to decorate methods. The information I have been able to find on-line focuses on decorating functions,

Re: Decorating methods - where do my arguments go?

2009-05-08 Thread Peter Otten
Mikael Olofsson wrote: Hi all! I have long tried to avoid decorators, but now I find myself in a situation where I think they can help. I seem to be able to decorate functions, but I fail miserably when trying to decorate methods. The information I have been able to find on-line focuses on