Luck Luck
Luck Luck

Reputation: 3

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?

Upvotes: 0

Views: 37

Answers (2)

Pyrce
Pyrce

Reputation: 8596

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 }
f(**kwargs)

Upvotes: 1

Greg Schmit
Greg Schmit

Reputation: 4574

You can call the function and pass arguments by name:

i = f(b=35,c=76)  # sets b and c while doing nothing to a

Upvotes: 0

Related Questions