Decorating class member functions

Andy Terrel andy.terrel at gmail.com
Thu May 3 21:21:36 EDT 2007


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()




More information about the Python-list mailing list