Decorating class member functions
Virgil Dupras
hardcoded.software at gmail.com
Thu May 3 21:41:57 EDT 2007
On May 3, 9:33 pm, Virgil Dupras <hardcoded.softw... at gmail.com> wrote:
> On May 3, 9:21 pm, Andy Terrel <andy.ter... at gmail.com> wrote:
>
>
>
> > Okay does anyone know how to decorate class member functions?
>
> > The following code gives me an error:
>
> > Traceback (most recent call last):
> > File "decorators2.py", line 33, in <module>
> > s.update()
> > File "decorators2.py", line 13, in __call__
> > retval = self.fn.__call__(*args,**kws)
> > TypeError: update() takes exactly 1 argument (0 given)
>
> > ------------------
>
> > #! /usr/bin/env python
>
> > class Bugger (object):
> > def __init__ (self, module, fn):
> > self.module = module
> > self.fn = fn
>
> > def __call__ (self,*args, **kws):
> > ret_val = self.fn(*args,**kws)
> > return ret_val
>
> > def instrument (module_name):
> > ret_val = lambda x: Bugger(module_name, x)
> > return ret_val
>
> > class Stupid:
> > def __init__(self):
> > self.val = 1
>
> > @instrument("xpd.spam")
> > def update(self):
> > self.val += 1
>
> > s = Stupid()
> > s.update()
>
> A decorator is a function that takes one single parameter: a function.
> "instrument" must return a decorator.
Oh wait, I just embarrassed myself. Nevermind my last post.
More information about the Python-list
mailing list