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