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 *

?

+3


source to share


2 answers


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.

+4


source


Only *

- it's only Python3 to express that the following parameters are called arguments and can only be passed to the function as such.

From the documentation:



Parameters after " *

" or " *identifier

" are keyword-only parameters and only keyword arguments can be passed.

+3


source







All Articles