Reputation: 1835
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)
Upvotes: 4
Views: 4848
Reputation: 108
In case someone is looking for passing dynamic parameters but those parameters are not key value pair arguments. You can create a dynamic
list
and pass it like this in function call
*list
e.g.
def fun(a,b,c,d):
pass
dynamic_list = []
if True:
dynamic_list.append(2)
if True:
dynamic_list.append(2)
if True:
dynamic_list.append(2)
if True:
dynamic_list.append(2)
fun(*dynamic_list)
Upvotes: 0
Reputation: 59118
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, '!!')
Upvotes: 12