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.

Positional-only parameters are function parameters that must be supplied strictly based on their order in the function call, explicitly prohibiting the use of keyword arguments (e.g., name=value) for those specific parameters. Introduced in Python 3.8 (PEP 570), they are defined in a function signature using a forward slash (/). All parameters declared before the / are enforced as positional-only. Parameters declared after the / retain standard Python behavior (they can be passed by position or by keyword) unless further modified by other signature operators.

Syntax

def function_name(pos_only_1, pos_only_2, /, standard_arg):
    pass

Mechanics and Behavior

When a function is invoked, the Python interpreter maps the provided arguments to the signature. If an argument corresponding to a positional-only parameter is passed using keyword syntax, the interpreter raises a TypeError.
def configure_matrix(rows, columns, /, fill_value):
    return [[fill_value] * columns for _ in range(rows)]


# VALID: 'rows' and 'columns' passed by position. 'fill_value' passed by position.
configure_matrix(3, 3, 0)


# VALID: 'rows' and 'columns' passed by position. 'fill_value' passed by keyword.
configure_matrix(3, 3, fill_value=0)


# INVALID: 'rows' and 'columns' passed by keyword.
configure_matrix(rows=3, columns=3, fill_value=0)

# TypeError: configure_matrix() got some positional-only arguments passed as keyword arguments: 'rows, columns'

Signature Composition

The / operator can be combined with the * operator (which enforces keyword-only parameters) to create a strictly partitioned function signature. This establishes clear boundaries for argument resolution:
def strict_function(pos_only, /, standard, *, kw_only):
    pass
  1. pos_only: Must be passed by position.
  2. /: Demarcates the end of positional-only parameters.
  3. standard: Can be passed by position or keyword.
  4. *: Demarcates the start of keyword-only parameters.
  5. kw_only: Must be passed by keyword.

Default Values

Positional-only parameters support default values. However, standard rules apply: a non-default positional-only parameter cannot follow a default positional-only parameter.

# VALID
def parse_data(data, strict=True, /, encoding="utf-8"):
    pass


# INVALID: Non-default argument follows default argument
def parse_data(data=None, strict, /, encoding="utf-8"):
    pass
Master Python with Deep Grasping Methodology!Learn More