Positional-Only and Keyword-Only Parameters
Core idea
In def pack(count, /, *, label):, count is positional-only and label is keyword-only. The slash marks the end of positional-only parameters; a bare star marks the start of keyword-only parameters. Ordinary parameters between the markers accept either form. Slash syntax is supported since Python 3.8.
def pack(count, /, *, label): return f"{label}: {count}"
print(pack(3, label="Box"))The count is positional and the label is named.
Reject a named positional-only parameter (intentional error)
def pack(count, /, *, label): return f"{label}: {count}"
pack(count=3, label="Box")The count cannot be supplied by keyword for this signature.
Reject positional keyword-only input (intentional error)
def pack(count, /, *, label): return f"{label}: {count}"
pack(3, "Box")The label must be supplied by name.
Common mistake
The slash and bare star are markers, not parameters or argument values. Do not insert them into calls.
Practice
Define report(value, /, *, unit) and try one valid and two invalid calls. Explain the binding rule for each.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Special parameter markers
Check your understanding of positional-only and keyword-only parameters.
What is printed?
def f(a, /, *, b): return a * bprint(f(2, b=3))What happens?
def f(a, /, *, b): return a * bf(a=2, b=3)What happens?
def f(a, /, *, b): return a * bf(2, 3)What does a bare * in a parameter list indicate?