例如,我有以下列表:
method = [fun1, fun2, fun3, fun4]
Run Code Online (Sandbox Code Playgroud)
然后我显示一个菜单,用户必须从1-4(len(method))中选择一个数字.如果用户选择i,我必须使用该功能funi.我怎样才能做到这一点?
例如.
A=['hello','bye','goodbye']
def hello(n):
print n**2
def bye(n):
print n**3
def goodbye(n):
print n**4
Run Code Online (Sandbox Code Playgroud)
如果我想bye通过数组A 调用该函数,请使用
>>>A[1](7)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
A[2](5)
TypeError: 'str' object is not callable
Run Code Online (Sandbox Code Playgroud)
如何使用保存的名称A来调用我的函数?因为每个方法都保存在一个字符串中.