如何传入JSON文件中定义的变量值?
我有两个功能:
def fun1(a , b) :
print(a , b)
def fun2(c , d) :
print(c , d)
jsonfile = {
'function1' : {
'callback' : 'fun1' ,
'args' : ['a' , 'b']
},
'function2' : {
'callback' : 'fun2',
'args' : ['c' , 'd']
}
}
# iter the json file looking for the function callback + the args to call them ..
for key , value in jsonfile.items() :
function_name = value['callback']
function_args = value['args']
locals()[function_name](# how to pass the functions args ?? )
当我这样做的时候:
locals()[function_name](function_args)
我得到以下错误,因为函数认为我只传递了1个参数,而不是2个:
locals()[function_name](function_args)
TypeError: fun1() missing 1 required positional argument: 'b'
有什么建议吗?