Luck Luck Luck Luck - 1 year ago 76
Python Question

How not to change the first default function parameter when changing the others

If I define a function with 2 or more default parameters, how can I change only the second or third (etc.) one when calling it? eg.:

>>>def f(a=10,b=5,c=7):
return (a*b*c)

How can I change the values of b or c if I want to keep the original value of a?

Answer Source

You can assign variables by name instead of position:

f(b=1, c=2)
f(0, c=2) # sets a=0 and c=2

Alternatively you can use key word arguments:

kwargs = { 'b': 1, 'c': 2 }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download