It is possible to put arbitrary arguments before * args. The following code passes.
optionalarg.py
def foo(x=10, *args, **kwargs):
return x, args, kwargs
print foo(12, 13, 14)
#=> (12, (13, 14), {})
However, it is not possible to define a required argument after an optional argument. The following code will result in a syntax error.
syntaxerror.py
def foo(x=10, y, *args, **kwargs):
return x, y, args, kwargs
# SyntaxError: non-default argument follows default argument
There are terms of optional arguments and named arguments, but how should we use them properly?
If you read "Dive In Python", you can guess as follows.
(Link: Translated version of cocoatomo because it is troublesome to read the English version) http://cocoatomo.iza-yoi.net/DIP/power_of_introspection/optional_arguments.html
Recommended Posts