Skip to main content
Keyword-only parameters are function arguments that can only be supplied using keyword syntax (parameter_name=value) during invocation. They cannot be bound to positional arguments. If a caller attempts to pass a keyword-only parameter positionally, the Python interpreter raises a TypeError. In a function signature, keyword-only parameters are defined by placing them after a var-positional parameter (typically *args) or after a bare asterisk (*).

Syntax Visualization

1. Using a Bare Asterisk (*) The bare asterisk consumes no arguments but dictates that all subsequent parameters in the signature are keyword-only.
2. Following a Var-Positional Parameter (*args) When a function accepts an arbitrary number of positional arguments via *args, any standard parameters defined after it automatically become keyword-only.

Parameter Ordering Rules

When constructing a function signature, Python enforces a strict parameter resolution order. Keyword-only parameters must be placed according to the following hierarchy:
  1. Positional-only parameters (preceding /)
  2. Standard parameters (positional or keyword)
  3. Var-positional parameter (*args) OR bare asterisk (*)
  4. Keyword-only parameters
  5. Var-keyword parameter (**kwargs)

Required vs. Optional

Keyword-only parameters can be required or optional.
  • If a keyword-only parameter is defined without a default value (e.g., secure in the first example), it is a required keyword-only parameter. Omitting it during invocation raises a TypeError: missing 1 required keyword-only argument.
  • If it is defined with a default value (e.g., timeout=30), it is optional.
Tired of Poor Python Skills? Fix That With Deep Grasping!Learn More