Decorating class member functions

Andy Terrel andy.terrel at gmail.com
Thu May 3 22:39:50 EDT 2007


not quite as elegant but here is a workaround... Thanks Virgil for
taking some time to think about it.

---

class Bugger (object):
    def __init__ (self, module):
        print "Entering __init__"
        self.module = module
        self.verb = 0

def instrument (module_name):
    def wrapper(f):
        def _wrap(*args,**kws):
            ret_val = f(*args,**kws)
            return ret_val
        return _wrap
    b = Bugger(module_name)
    if b.verb == 0:
        ret_val = wrapper
    else:
        ret_val = lambda x:x
    return ret_val

class Stupid:
    def __init__(self):
        self.val = 1

    @instrument("spam")
    def update(self):
        self.val += 1


s = Stupid()
s.update()
s.update()
s.update()
s.update()
print s.val




More information about the Python-list mailing list