Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
def configure_server(host, port, *, secure, timeout=30):
    # 'host' and 'port' can be positional or keyword.
    # 'secure' and 'timeout' are strictly keyword-only.
    pass


# Valid Invocation
configure_server("127.0.0.1", 8080, secure=True)
configure_server("127.0.0.1", 8080, secure=True, timeout=60)


# Invalid Invocation: Raises TypeError (takes 2 positional arguments but 3 were given)
configure_server("127.0.0.1", 8080, True) 
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.
def concatenate_strings(*strings, delimiter=","):
    # 'strings' captures all positional arguments as a tuple.
    # 'delimiter' is keyword-only.
    return delimiter.join(strings)


# Valid Invocation
concatenate_strings("apple", "banana", "cherry", delimiter="|")


# Syntactically valid, but "->" is absorbed into the *strings tuple, 

# leaving 'delimiter' at its default value.
concatenate_strings("apple", "banana", "cherry", "->") 

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)

# Comprehensive Signature Example
def complex_function(pos_only, /, standard, *args, kw_only1, kw_only2="default", **kwargs):
    pass

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.
Master Python with Deep Grasping Methodology!Learn More