All required parameters must be placed before any default arguments. Simply because they are mandatory, whereas default arguments are not. Syntactically, it would be impossible for the interpreter to decide which values match which arguments if mixed modes were allowed. A SyntaxError
is raised if the arguments are not given in the correct order:
Let us take a look at keyword arguments, using your function.
def fun1(a="who is you", b="True", x, y):
... print a,b,x,y
Suppose its allowed to declare function as above,
Then with the above declarations, we can make the following (regular) positional or keyword argument calls:
func1("ok a", "ok b", 1) # Is 1 assigned to x or ?
func1(1) # Is 1 assigned to a or ?
func1(1, 2) # ?
How you will suggest the assignment of variables in the function call, how default arguments are going to be used along with keyword arguments.
>>> def fun1(x, y, a="who is you", b="True"):
... print a,b,x,y
...
Reference O'Reilly - Core-Python
Where as this function make use of the default arguments syntactically correct for above function calls.
Keyword arguments calling prove useful for being able to provide for out-of-order positional arguments, but, coupled with default arguments, they can also be used to "skip over" missing arguments as well.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…