小编Lui*_*ipe的帖子

如何从列表中调用函数?

例如,我有以下列表:

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来调用我的函数?因为每个方法都保存在一个字符串中.

python function

2
推荐指数
1
解决办法
893
查看次数

标签 统计

function ×1

python ×1