If you use one equal sign, use them on everything.
This can be quite confusing, the following mess shows why:
The actual rule is that:
any required arguments must have values, and order must be preserved if they do not use '='.
If = is used on both, they can be out of order.
but once an equal sign has been used, all remaining required arguments must also have one.
Non required arguments can be in any order if and only if they contain '=' signs.
Non required arguments don't require '=' if all arguments remain in order
An example will make this clearer:
c, d and e are defaulted, so don't have to be supplied
and can be in any order so long as they contain an = sign
a and b are, required and must be in order
as will:
and finally:
Bottom line is Always use assignment ('=') it will save you grief
This can be quite confusing, the following mess shows why:
The actual rule is that:
any required arguments must have values, and order must be preserved if they do not use '='.
If = is used on both, they can be out of order.
but once an equal sign has been used, all remaining required arguments must also have one.
Non required arguments can be in any order if and only if they contain '=' signs.
Non required arguments don't require '=' if all arguments remain in order
An example will make this clearer:
c, d and e are defaulted, so don't have to be supplied
and can be in any order so long as they contain an = sign
a and b are, required and must be in order
>>> def xxx (a, b, c=0, d=1, e=2): ... print(a) ... print(b) ... print(c) ... print(d) ... print(e)The function above requires and a and b at minimum, so:
>>> xxx(a=1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: xxx() missing 1 required positional argument: 'b'creates error because b is required and missing
>>> xxx(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: xxx() missing 1 required positional argument: 'b'creates error because b is required and missing
>>> xxx(1, b=2) 1 2 0 1 2 >>>Works because both a and b have been passed
as will:
xxx(1, 2)but not:
>>> xxx(a=2,3) File "<stdin>", line 1 SyntaxError: positional argument follows keyword argument >>> because once '=' has been used for oand the following will work:
>>> xxx(a=2, b=3) 2 3 0 1 2 >>>
>>> xxx(1,2,3, e=7) 1 2 3 1 7 >>>works because 1,2,3 are in order and e contains =
and finally:
>>> xxx(1,2,c=3,4,e=7) File "<stdin>", line 1 SyntaxError: positional argument follows keyword argument >>> xxx(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: xxx() missing 1 required positional argument: 'b'does not. Once the first defaulted argument contains an = sign, all that follow also require it
Bottom line is Always use assignment ('=') it will save you grief