简体   繁体   English

通过名称动态传递参数

[英]Pass in argument to function by name dynamically

Is it possible to dynamically set the name of an argument being passed into a function? 是否可以动态设置传递给函数的参数的名称?

like this: 像这样:

def func(one=None, two=None, three=None, four=None):
    ...

params = ("one","two","three","four",)
for var in params:
    tmp = func(var=value)

Yes, with keyword argument unpacking : 是的,使用关键字参数解压缩

def func(one=None, two=None, three=None, four=None):
    return (one, two, three, four)

params = ("one", "two", "three", "four")
for var in params:
    tmp = func(**{var: "!!"})
    print(tmp)

Output: 输出:

('!!', None, None, None)
(None, '!!', None, None)
(None, None, '!!', None)
(None, None, None, '!!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python将函数名称作为参数传递 - Python pass function name as argument Python动态地将参数传递给字符串格式函数 - Python dynamically pass argument to string format function 如何将参数名称传递给函数的变量名称 - How to pass a name of an argument to the variable names of a function 传递函数名称作为参数,然后检查正确性 - pass a function name as argument and then check correctness Python-如何将动态系列名称和DataFrame名称作为函数参数传递? - Python - how to pass a dynamic Series name and DataFrame name as a function argument? Python-用户输入的变量名称作为参数传递给函数 - Python - User input for name of a variable to pass as an argument to a function 当我在函数中传递一个完全不同的参数名称时会发生什么? - What happens when I pass a totally different argument name in a function? 如何将字符串转换为参数名称并将其传递给函数? - How can convert a string into an argument name and pass it to a function? 如何传递与 function 的位置参数同名的 kwarg? - How to pass kwargs with the same name as a positional argument of a function? 如果我们传递 Python function 名称,究竟传递了什么作为参数? - What is exactly passed as an argument if we pass Python function name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM