Keyword only parameter [duplicate]

No, you must use either the * bare parameter, or use a single *args parameter, called a var-positional parameter (see the next item in that glossary entry). By adding it to your function signature you force any parameters that follow it to be keyword-only parameters.

So the function signature could be:

def func(positional_arg1, *variable_args, kw_only1, kw_only2):

and variable_args will capture any extra positional arguments passed to the function, or you could use:

def func(positional_arg1, *, kw_only1, kw_only2):

and the function will not support extra positional arguments beyond the first one.

In both cases, you can set kw_only1 and kw_only2 only by using them as keyword arguments when calling func(). Without default values (no =<expression> in their definition) they are still required arguments.

Leave a Comment