0

I'm writing a python script. In the script I wrote a function with some arguments that all has defult values. Also in the script i have list of tuples, first item in each tuple its the name of the argument from the function, the second is the value. here is code for example:

def f(a=0, b=0, c=0):
 print(a), print(b), print(c)
args = [('a', 12), ('c', -64)]

Is there some way, when I call the method, to put the value 12 in the 'a' argument and the value -64 in the 'c' argument without do it manualy? (the value b remains 0).

Thanks for any Help!

asked Apr 22, 2020 at 18:42

2 Answers 2

5

You can build a dictionary then unpack it with **:

f(**dict(args))

will output

12
0
-64
answered Apr 22, 2020 at 18:44
Sign up to request clarification or add additional context in comments.

Comments

-1
def f(a=0, b=0, c=0):
 print(a), print(b), print(c)
args = [('a', 12), ('c', -64)]
f(**dict(args))

result:

a = 12
b = 0
c = -64

But better way to use dict

answered Apr 22, 2020 at 18:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.