Ordering Arguments in a Function
00:00
Now that you’ve learned what *args
and **kwargs
are for, you’re ready to start writing functions that take a varying number of input arguments.
00:07
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 **kwargs
.
00:22 So the correct order for your parameters is this: standard arguments come first,
00:29
followed by *args
arguments, and then finally **kwargs
arguments. So for example, this function definition is correct. The *args
variable is appropriately listed before **kwargs
.
00:41
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.
00:50
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 SyntaxError
.
Become a Member to join the conversation.