0

我有课:

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 定义主体中调用方法名称?

4

3 回答 3

2

你必须明确地传递它:

class Transplant:
    def __init__(self, method, host, method_name=None):
        self.host = host
        self.method = method
        self.method_name = method_name or method.__name__
        setattr(host, method_name or method.__name__, self)

    def __call__(self, *args, **kwargs):
        nargs = [self.host, self.method_name]
        nargs.extend(args)
        return apply(self.method, nargs, kwargs)

并扩展foo以接受它作为论据。

于 2013-02-04T11:19:26.357 回答
1

你看过getattr吗?

getattr(self, "method")
于 2013-02-04T11:14:58.833 回答
0

您可以使用函数工厂来做到这一点make_foo

关键步骤是重新定义函数的func_name属性:

foo.func_name = name

class SimpleClass:
    def init_levels(self):
        levels = ['foo', 'bar']
        for level in levels:
            # Your original code defined this on `SimpleLog`. Did you mean `SimpleClass`?
            setattr(SimpleClass, level, make_foo(level))

def make_foo(name):
    def foo(self):
        print('{n} has been called'.format(n = foo.func_name))

    foo.func_name = name
    return foo

simpleinst = SimpleClass()
simpleinst.init_levels()
simpleinst.foo()
# foo has been called
simpleinst.bar()
# bar has been called
于 2013-02-04T11:17:48.357 回答