Python 如何将键、值对从函数参数传递到另一个函数?

Python 如何将键、值对从函数参数传递到另一个函数?,python,Python,人为的例子: from otherplace import enhance_thing def modify_thing(mult=False, add=False, sub=False): arguments = locals() thing = 1 for key,value in arguments.items(): if not value: continue thing = enhance_thing(t

人为的例子:

from otherplace import enhance_thing

def modify_thing(mult=False, add=False, sub=False):
    arguments = locals()
    thing = 1
    for key,value in arguments.items():
        if not value:
            continue
        thing = enhance_thing(thing, key=value)
    return thing
首先,如果您不熟悉
参数
将成为输入关键字参数的字典,默认为
{“mult”:False,“add”:False,“sub”:False}

现在我希望
modify\u-thing(add=3,mult=5)
函数调用
enhanced\u-thing
2次,首先作为
enhanced\u-thing(1,add=3)
然后作为
enhanced\u-thing(4,mult=5)
并最终返回20


但是,它只会调用
enhanced\u thing(1,key=3)
,然后返回一个错误。如何将关键字
key
更改为实际变量?

如果要直接传递关键字参数,最好使用
kwargs
捕获所有关键字参数,然后一次传递一个:

from otherplace import enhance_thing

def modify_thing(**kwargs):
    thing = 1
    # kwargs will become a dict of {'add': 3, 'mult': 5} in your example
    for key, value in kwargs.items():
        if not value:
            continue
        # **{key: value} will produce the dynamic `key=value` syntax you want where key and value are replaced by the actual keyword arguments
        # in your example, it will create enhance_thing(thing, add=3) and then enhance_thing(thing, mult=5)
        thing = enhance_thing(thing, **{key: value}) # this will pass the individual keyword arguments through
    return thing

如果要直接传递关键字参数,最好使用
kwargs
捕获所有关键字参数,然后一次传递一个:

from otherplace import enhance_thing

def modify_thing(**kwargs):
    thing = 1
    # kwargs will become a dict of {'add': 3, 'mult': 5} in your example
    for key, value in kwargs.items():
        if not value:
            continue
        # **{key: value} will produce the dynamic `key=value` syntax you want where key and value are replaced by the actual keyword arguments
        # in your example, it will create enhance_thing(thing, add=3) and then enhance_thing(thing, mult=5)
        thing = enhance_thing(thing, **{key: value}) # this will pass the individual keyword arguments through
    return thing

我认为这里的问题是locals()包含很多变量,而不仅仅是modify_thing中作为参数接收的变量

可能的解决方案是:

from otherplace import enhance_thing

def modify_thing(mult=False, add=False, sub=False):
    arguments = {"mult": mult, "add": add, "sub": sub}
    thing = 1
    for key,value in arguments.items():
        if not value:
            continue
        thing = enhance_thing(thing, key=value)
    return thing
但它会导致代码重复。也许最好使用以下解决方案:

from otherplace import enhance_thing

def modify_thing(**kwargs):
    arguments = kwargs
    # Maybe some assertation about what kwargs contains
    thing = 1
    for key,value in arguments.items():
        if not value:
            continue
        thing = enhance_thing(thing, key=value)
    return thing
此外,“thing”变量没有累积。我猜你是想用+=而不是=

总结如下:

from otherplace import enhance_thing

def modify_thing(**kwargs):
    arguments = kwargs
    # Maybe some assertation about what kwargs contains
    thing = 1
    for key,value in arguments.items():
        if not value:
            continue
        thing += enhance_thing(thing, key=value)
    return thing

祝你好运

我认为这里的问题是locals()包含很多变量,而不仅仅是modify_thing中作为参数接收的变量

可能的解决方案是:

from otherplace import enhance_thing

def modify_thing(mult=False, add=False, sub=False):
    arguments = {"mult": mult, "add": add, "sub": sub}
    thing = 1
    for key,value in arguments.items():
        if not value:
            continue
        thing = enhance_thing(thing, key=value)
    return thing
但它会导致代码重复。也许最好使用以下解决方案:

from otherplace import enhance_thing

def modify_thing(**kwargs):
    arguments = kwargs
    # Maybe some assertation about what kwargs contains
    thing = 1
    for key,value in arguments.items():
        if not value:
            continue
        thing = enhance_thing(thing, key=value)
    return thing
此外,“thing”变量没有累积。我猜你是想用+=而不是=

总结如下:

from otherplace import enhance_thing

def modify_thing(**kwargs):
    arguments = kwargs
    # Maybe some assertation about what kwargs contains
    thing = 1
    for key,value in arguments.items():
        if not value:
            continue
        thing += enhance_thing(thing, key=value)
    return thing

祝你好运

这并不能解决OP的问题,其中
key
将被解释为关键字参数名称,而不是根据原始关键字参数创建动态参数。这并不能解决OP的问题,其中
key
将被解释为关键字参数名称,不是根据原始关键字ArgumentsHanks创建动态参数,而是将
thing=Enhanced\u thing(thing,key=value)
修改为
thing=Enhanced\u thing(thing,**{key:value})
。不幸的是,由于我使用某个库的方式,我无法在外部函数中使用
**kwargs
,但这确实有效。再次感谢。@Chris啊,我明白了。没问题,很高兴另一部分为YouTunks工作,这只是将
thing=enhance\u thing(thing,key=value)
修改为
thing=enhance\u thing(thing,**{key:value})
。不幸的是,由于我使用某个库的方式,我无法在外部函数中使用
**kwargs
,但这确实有效。再次感谢。@Chris啊,我明白了。没问题,很高兴另一部分为你工作