Calling instance methods from a decorator
Kirk Strauser
kirk at daycos.com
Fri May 30 13:19:56 EDT 2008
I'm trying to write a decorator that would do something like:
def trace(before, after):
def middle(func):
def inner(*args, **kwargs):
func.im_self.debugfunction(before)
result = func(*args, **kwargs)
func.im_self.debugfunction(after)
return result
return inner
return middle
class Foo(object):
def __init__(self, myname):
self.name = myname
def debugfunction(self, message):
print 'Instance %s says: %s' % (self.name, message)
@trace('calling', 'finished')
def bar(self, arg):
print arg
>>> Foo('snake').bar(123)
Instance snake says: calling
123
Instance snake says: finished
The gotcha seems to be that there's no way to get to 'self' from within the
"inner" function, since func will only have the "normal" attributes:
>>> print dir(func)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__module__', '__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
There's no nice im_self to bounce off of or anything. I seem to be going
about this all wrong. What's a good approach to get the desired effect?
--
Kirk Strauser
The Day Companies
More information about the Python-list
mailing list