Python 3.x 使用args从dict调用函数
我想从字典中调用函数Python 3.x 使用args从dict调用函数,python-3.x,Python 3.x,我想从字典中调用函数 def target(Enemy): hero.target = Enemy commands = { 'close':close, 'weapon attack':wpnatk, 'spell attack':splatk, 'flee':flee, 'look':look, 't
def target(Enemy):
hero.target = Enemy
commands = {
'close':close,
'weapon attack':wpnatk,
'spell attack':splatk,
'flee':flee,
'look':look,
'target':target
}
inp = str(input('>>'))
if inp in commands:
commands[inp]()
如何将参数推送到函数?如果我记得你没有在字典中添加括号。只需在末尾添加括号:
def target(n):
print(n)
functions = {
'function':target
}
functions['function'](1)
您可以像对待“普通”函数一样“推送参数”:将其放在括号中。它可以从if语句调用。输入拆分是否会与命令[inp](inp[-1])一起工作,从而使输入的最后一部分成为参数?除了仅适用于“目标龙”之类的对象,但逃跑或关闭不会以这种方式工作