1 回答
TA贡献1815条经验 获得超13个赞
这是一种使用检查TheirWrapperwith内容的可能方法dir():
import inspect
class TheirWrapper:
def __init__(self):
pass
def func1(self, a, b, c):
pass
def func2(self, d, e, f):
pass
class MyWrapper:
def addToQueue(self, localvars):
# your implementation
print(localvars)
### You can orginize this block into decorator or metaclass and make more general
def add_func(func_name):
def add(self, *args, **kwargs):
signature = inspect.signature(getattr(TheirWrapper, func_name))
bind = signature.bind(self, *args, **kwargs)
arguments = dict(bind.arguments)
arguments['func_name'] = func_name
self.addToQueue(arguments)
return add
for name in dir(TheirWrapper):
if not name.startswith('__'):
setattr(MyWrapper, name, add_func(name))
###
w = MyWrapper()
w.func1(1, 2, 3)
# prints {'self': <__main__.MyWrapper object at 0x00000000030262E8>, 'a': 1, 'b': 2, 'c': 3, 'func_name': 'func1'}
添加回答
举报