Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将任意数量的参数传递给Flask中的python函数_Python_Flask - Fatal编程技术网

将任意数量的参数传递给Flask中的python函数

将任意数量的参数传递给Flask中的python函数,python,flask,Python,Flask,我正在使用烧瓶。我想使用全局调用函数 我使用作为字典传递的参数进行API调用: my_parameters = { "function" : "add_numbers", "number_1" : 100, "number_2" : 200 } 我可以调用Flask中的函数,如下所示: @app.route("/call_my_function/", methods=["GET"]) def call_my_function(): res = g

我正在使用烧瓶。我想使用全局调用函数

我使用作为字典传递的参数进行API调用:

my_parameters = {
    "function" : "add_numbers",
    "number_1" : 100,
    "number_2" : 200
}
我可以调用Flask中的函数,如下所示:

@app.route("/call_my_function/", methods=["GET"])
    def call_my_function():
        res = globals()[request.args.get('function')](100, 200)
        return(res)
…这很好,但正如您所看到的,我正在对100和200个参数进行编码。我希望这些参数从
我的\u参数
字典中传递。我试过这个:

@app.route("/call_my_function/", methods=["GET"])
    def call_my_function():
        res = globals()[request.args.get('function')](list(args.values())[1:])
        return(res)
但这些论点在globals中不被接受。它说:

TypeError: add_numbers() missing 1 required positional argument

使用tuple unpack应该有神奇的作用:
globals()[request.args.get('function')](*args.values())
这不会从参数对象中删除“function”参数。有没有办法从*args.values()中删除第一个元素?换言之,这会导致“获取2个位置参数,但3个已给定”错误。元组不可变的问题。首先将值作为list
values=list(args.values())[1://code>获取,然后作为tuple
globals()[request.args.get('function')](*元组(values))
传递就是这样。非常感谢。很高兴听到:)