python如何参数自动解包到需要的位置?

比如说有个orm查询的函数
filter(name="hello",sex = 1)

name和sex这种都是根据前端所请求的参数确定的,
那如果前端再多加了一个条件
比如 age

那么现在就是filter(name="hello",sex = 1,age = 18)
后端就应该写成

if name:
    if sex:
        if age:
            filter(name="hello",sex = 1,age = 18)
        else:
            filter(name="hello",sex = 1)
    else:
        filter(name="hello")
if sex:
    ....

能不能根据前端传过来的参数数量
来动态传递这个参数
比如 parm = {"name" : "hello" , "age" : 18 , "sex" : 1}
传到 filter

阅读 1.4k
1 个回答
filter(**parm)
推荐问题