根据参数动态调用嵌套函数

2 投票
2 回答
1343 浏览
提问于 2025-04-16 07:35

如果我有以下这个Python类:

class Test(object):
    funcs = {
        "me"    : "action",
        "action": "action",
        "say"   : "say",
        "shout" : "say"
    }

    def dispatch(self, cmd):
        def say:
            print "Nested Say"

        def action:
            print "Nested Action"

        # The line below gets the function name as a string,
        # How can I call the nested function based on the string?
        Test.funcs.get(cmd, "say")

我想要能够做到以下这些:

>>> Test().dispatch("me")
Nested Action
>>> Test().dispatch("say")
Nested Say

有没有什么建议可以帮我实现这个目标?

2 个回答

2
class Test(object):

    def dispatch(self):
        def say():
            print "Nested Say"

        def action():
            print "Nested Action"

        funcs = {
            "me"    : action,
            "action": action,
            "say"   : say,
            "shout" : say
        }

        Test.funcs.get(cmd, say)()

或者,保持你现在的结构:

class Test(object):

    funcs = {
        "me"    : "action",
        "action": "action",
        "say"   : "say",
        "shout" : "say"
        }

    def dispatch(self, cmd):
        def say():
            print "Nested Say"

        def action():
            print "Nested Action"

        locals()[Test.funcs.get(cmd, "say")]()

不过,我觉得这个设计有点奇怪。为什么类里面的字典要知道调度(dispatch)里的局部函数呢?

4

我可能会这样做:

def register(dict_, *names):
    def dec(f):
        m_name = f.__name__
        for name in names:
            dict_[name] = m_name
        return f
    return dec

class Test(object):

    commands = {}

    @register(commands, 'foo', 'fu', 'fOo')
    def _handle_foo(self):
        print 'foo'

    @register(commands, 'bar', 'BaR', 'bAR')
    def _do_bar(self):
        print 'bar'

    def dispatch(self, cmd):
        try:
            return getattr(self, self.commands[cmd])()
        except (KeyError, AttributeError):
            # Command doesn't exist. Handle it somehow if you want to
            # The AttributeError should actually never occur unless a method gets 
            # deleted from the class

现在,这个类提供了一个 dict,它的键是用来测试某个东西是否属于某个集合的命令。所有的方法和这个字典只会创建一次。

t = Test()

if 'foo' in t.commands:
    t.dispatch('foo')

for cmd in t.commands:
    # Obviously this will call each method with multiple commands dispatched to it once
    # for each command
    t.dispatch(cmd)

等等。

撰写回答