开发者

Dynamically calling nested functions based on arguments

If I have the following Python cla开发者_如何学Pythonss:

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")

I would like to be able to do the following:

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

Any suggestions as to how I can go about this?


I would probably do something like this:

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

Now, the class exposes a dict whose keys are commands for testing membership. All the methods and the dictionary are created only once.

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)

Etc.


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)()

Or, keeping your current structure:

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")]()

I find this design a bit odd, though. Why should the class-level dict know about dispatch's local functions?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜