I am reading the source codes of mercurial, and found such a func def in commands.py:
def import_(ui, repo, patch1=None, *patches, **opts):
...
in python, postional args must be put ahead of keyword args. But here, patch1 is a keyword argument, followed by a positional argument *patches. why is this OK?
Just have a look into the PEP 3102 also it seems its somehow related to this.
To summarize, patches and opts are there to accept variable arguments but the later is to accept keyword arguments. The keyword arguments are passed as a dictionary where as the variable positional arguments would be wrapped as tuples .
From your example
Any positional parameters after
u1,repo and patch1would be wrapped as tuples in patches. Any keyword arguments following the variable positional arguments would be wrapped as Dictionary objects through opts.Another important thing is that, the onus lies with the caller to ensure that the condition
non-keyword arg after keyword argis not violated.So something that violates this would raise a syntax error..
For example
Calls like
are valid, but
would raise a syntax error
SyntaxError: non-keyword arg after keyword argIn the first valid case
import_(1,2,3,test="test")In the second valid case
import_(1,2,3,patch1=4,test="test")In the third valid case
import_(1,2,3,4,5)In the fourth valid case
import_(1,2,patch1=3,test="test")