我有课:
class SimpleClass:
def __init__(self):
pass
def init_levels(self):
levels = get_levels_list()
for level in levels:
Transplant(foo, Simplelog, method_name=level)
Transplant 是一个为类动态添加方法的类:
class Transplant:
def __init__(self, method, host, method_name=None):
self.host = host
self.method = method
self.method_name = method_name
setattr(host, method_name or method.__name__, self)
def __call__(self, *args, **kwargs):
nargs = [self.host]
nargs.extend(args)
return apply(self.method, nargs, kwargs)
Foo 是“移植”的函数:
def foo(self):
return
如何在 foo 中调用方法名称?
例如我执行:
simpleinst = SimpleClass()
simpleinst.init_levels()
如何修改我的代码以在 foo 定义主体中调用方法名称?