Advertisement
Advanced Python, Functions & Scope

How do positional-only parameters (using a bare '/' in the parameter list, Python 3.8+) restrict how certain parameters can be passed?

Short answer

Placing a bare '/' in the parameter list marks every parameter BEFORE it as positional-only — callers CANNOT pass them by keyword name, useful for parameters whose NAME isn't meant to be part of the function's stable public API (letting you rename it later without breaking…

Save Python mock interview
Advertisement
AModel answer
Placing a bare '/' in the parameter list marks every parameter BEFORE it as positional-only — callers CANNOT pass them by keyword name, useful for parameters whose NAME isn't meant to be part of the function's stable public API (letting you rename it later without breaking callers who used the keyword form).
def divide(a, b, /):
    return a / b

divide(10, 2)          # OK: positional
# divide(a=10, b=2)    # Error: 'a' and 'b' are positional-only, can't use as keywords
Real-world example
Preventing callers from depending on a parameter's exact NAME as part of the function's API contract, giving you freedom to rename it later.
Likely follow-ups
  • How would you combine BOTH '/' and '*' in the same function signature to have positional-only, normal, AND keyword-only parameters?
Related topics