Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 846
Jeremy Avatar asked Dec 20 '22 08:12

Jeremy


1 Answers

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, '!!')
like image 108
Zero Piraeus Avatar answered Dec 21 '22 22:12

Zero Piraeus