小编Ama*_*ria的帖子

有没有办法让函数作为字典的值?

我必须通过从用户那里获取有关将作为字符串执行的操作的输入来执行多项操作。所以我在考虑使用一个字典,其中函数名是键,值是函数本身。我还想将参数传递给函数。有没有办法做到这一点?这就是我想要做的:

a = [] # an empty list
inp = input() # which function to apply?
arg = int(input()) # the argument corresponding to the function.

# a dictionary mapping all the functions to the names
func_dict = {"append":a.append(), "pop":a.pop(), "extend": a.extend()} 
Run Code Online (Sandbox Code Playgroud)

我知道这行不通,我也想给出用户输入的参数。有没有办法做到这一点?

谢谢。

python dictionary function list python-3.x

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

标签 统计

dictionary ×1

function ×1

list ×1

python ×1

python-3.x ×1