I need to pass several methods as callbacks which don't take the self
argument. This is how my current code looks like:
def _do_callback(callback, log, *args):
try:
# some common code
callback(*args)
except:
log.error('XX')
class Foo(object):
def __init__(self):
self.log = Log('Foo')
self.cb1_wrapper = lambda x: _do_callback(self.cb1, self.log, x) # I need a function taking one parameter
self.cb2_wrapper = lambda x: _do_callback(self.cb2, self.log, x)
def cb1(self, x):
# some code accessing self
def cb2(self, x):
# some code accessing self
def register_callbacks(self):
register('1', self.cb1_wrapper)
register('2', self.cb2_wrapper)
Is it possible to write some decorator to apply to cb1
and cb2
to be able to pass the result into code that currently takes self.cb1_wrapper
?
(I know the title is not ideal, feel free to edit)