Python positional arguments
I'm trying to figure out how the parameters of this are interpreted function
:
def f(a, *, b):
return a, b
It looks like this function
one makes the caller call f()
with exactly two parameters, and the second parameter should always have a named parameter b=
. How do you decipher this from the signature function
? Why doesn't this allow me to specify the middle argument for *
?
source to share
How can I decipher this from the function signature?
- Arguments without a default must be passed.
- Arguments after
*
must be passed by keyword, if passed at all. - Additional arguments cannot be passed to "fill"
*
if the parameter name does not match*
.
Since b
there is no default, it must be passed. Since after *
it must be passed by keyword. Since it *
is "naked" (ie, it is just a *
placeholder, not a vararg like *args
), no other positional arguments can be passed as "middle" arguments.
See PEP 3102 for a description of the keyword-only syntax.
source to share