[英]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.