Ordering Arguments in a Function
Now that you’ve learned what
**kwargs are for, you’re ready to start writing functions that take a varying number of input arguments.
But what if you want to create a function that takes a changeable number of both positional and named arguments? In this case, you have to bear in mind that order counts. Just as non-default arguments have to precede default arguments, so
*args must come before
00:22 So the correct order for your parameters is this: standard arguments come first,
*args arguments, and then finally
**kwargs arguments. So for example, this function definition is correct. The
*args variable is appropriately listed before
But what if you try to modify the order of the arguments? For example, have a look at this function. Now,
**kwargs comes before
*args in the function definition.
If you try to run this example, you’ll receive an error from the interpreter. In this case, since
*args comes after
**kwargs, the Python interpreter throws a
Become a Member to join the conversation.