Programming Search⌘K

Featurecore

Positional-only parameters

Also called positional-only parameter, slash in def, / in parameter list, PEP 570, POSITIONAL_ONLY, takes no keyword arguments.

Parameters before a / in a def can be filled only by position. A caller cannot pass them as keywords, and a **kwargs parameter can take the same names.

Language
Python

Synopsis

def pos_only_arg(arg, /):
    print(arg)

pos_only_arg(1)
try:
    pos_only_arg(arg=1)
except TypeError:
    print("arg is positional-only")
Output
1
arg is positional-only

The / makes arg positional-only. A call with arg=1 is a TypeError.

Semantics

r1. "If positional-only, the parameters’ order matters, and the parameters cannot be passed by keyword. Positional-only parameters are placed before a / (forward-slash)."1

r2. "Parameters following the / may be positional-or-keyword or keyword-only."1 Example: after-slash.

r3. "Positional-only parameters have no externally-usable name. When a function accepting positional-only parameters is called, positional arguments are mapped to these parameters based solely on their order."2

r4. "Once a positional-only parameter is specified with a default, the following positional-only and positional-or-keyword parameters need to have defaults as well."2 Example: default-order.

r5. "the names of positional-only parameters can be used in **kwds without ambiguity."1 Example: name-in-kwds.

r6. "Use positional-only if you want the name of the parameters to not be available to the user. This is useful when parameter names have no real meaning, if you want to enforce the order of the arguments when the function is called or if you need to take some positional parameters and arbitrary keywords."1

Examples

def clamp(value, /, low=0, high=10):
    return max(low, min(value, high))

print(clamp(15), clamp(-3, low=-5), clamp(5, 6, 8))
Output
10 -3 6

low and high come after /, so a call can give them by position or by name (rule r2).

def foo(name, /, **kwds):
    return "name" in kwds

print(foo(1, **{"name": 2}))
Output
True

This is the example of the tutorial and PEP 570. The positional-only name and the key "name" do not collide (rule r5).

try:
    len(obj="abc")
except TypeError:
    print("keywords not accepted")
print(len("abc"))
Output
keywords not accepted
3

The built-in len() takes its argument only by position. With /, a function written in Python can do the same (rule r1).

Pitfalls

Without /, the name of the first parameter collides with a key of **kwds:

This code raises TypeError, on purpose.

def foo(name, **kwds):
    return "name" in kwds

foo(1, **{"name": 2})

A positional-only parameter with a default needs defaults for the parameters after it, up to *:

This code raises SyntaxError, on purpose.

def name(p1=None, p2, /):
    pass

Comparison

Go parameters are all positional. A Go signature can even leave the parameters without names: "Within a list of parameters or results, the names (IdentifierList) must either all be present or all be absent."4 So a Go parameter name is never part of what a caller writes, as with a Python positional-only parameter (rule r3).

References

  1. The Python Tutorial, 4. More Control Flow Tools, 4.9.3.2. Positional-Only Parameters; 4.9.3.4. Function Examples; 4.9.3.5. Recap
  2. PEP 570: Python Positional-Only Parameters, Abstract; Specification; Semantic Corner Case
  3. The Python Language Reference, Python 3.15, 8.7. Function definitions
  4. The Go Programming Language Specification, Function types

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source