Can keyword arguments be used positionally?

I have a similar question. In the quiz, a function is defined as follows:
def update(new_value = 10):
old_value = new_value

It asks how to call the function with new_value of 20.

The correct answer is
update(20)

So does this work because there is only one parameter for the function, so that the keyword new_value doesn’t need to be explicitly spelled out when calling the function? If there were more parameters, each with a default value, or some has default values and others don’t, how would it work? Would the keyword arguments need to be spelled out in those cases?

thanks

1 Like

new_value is only a local variable, not accessible outside of the function. It is preset to 10, but takes on any value we pass in. If no value is passed in, then new_value becomes 10.

Multiple keywords are possible, but the position is critical.

>>> def foo(a = 1, b = 2, c = 3):
    return a * 4, b * 3, c * 2

>>> foo()
(4, 6, 6)
>>> foo(4)
(16, 6, 6)
>>> foo(4, 5)
(16, 15, 6)
>>> foo(4, 5, 6)
(16, 15, 12)
>>> 
16 Likes

Such a clear explanation! Thank you!

1 Like