如何在字典中传递多个函数参数?

时间:2014-01-02 10:18:40

标签: python dispatch

我已经使用了这种模式, 但我现在正在尝试将多个参数传递给函数 我应该添加另一个参数还是我的另一个语法片段 丢失?

    def newChannel(hname, cname):
        pass

    action = {'newChannel': (newChannel, hname),
         'newNetwork': (newNetwork, cname) , 'loginError': (loginError, nName)}

    handler, param = action.get(eventType)
    handler(param)

我怎么能通过多个参数呢? 像这样的......

    action = { 'newChannel': (newChannel, hname, cname) } 

是正确的吗?

编辑: 这会飞吗?

    action = {'newChannelQueueCreated': (newChannelQueueCreated, (channelName, timestamp)), 'login':    
    (login, networkName), 'pushSceneToChannel': (pushSceneToChannel, channelName),  
    'channelRemovedFromNetwork': (channelRemovedFromNetwork, (channelName, timestamp))}
     print ok 
     handler, getter  = action.get(eventType(ok))
     handler(*getter(ok)) 

1 个答案:

答案 0 :(得分:6)

为什么不使用元组?

action = {'newChannel': (newChannel, (hname, cname)),
          'newNetwork': (newNetwork, (cname,)),
          'loginError': (loginError, (nName,))}

handler, params = action.get(eventType)
handler(*params)