Programming Search⌘K

Featurecore

Positional-or-keyword parameters

Also called positional or keyword parameter, standard parameter, normal parameter, POSITIONAL_OR_KEYWORD, pass by position or by name.

A parameter that is not before / and not after * is positional-or-keyword. A call can fill it by position or by its name. This is the default kind, so a plain def has only this kind of parameter.

Language
Python

Synopsis

def standard_arg(arg):
    print(arg)

standard_arg(2)
standard_arg(arg=2)
Output
2
2

The tutorial calls this "the most familiar form". It "places no restrictions on the calling convention and arguments may be passed by position or keyword".1

Semantics

r1. "If / and * are not present in the function definition, arguments may be passed to a function by position or by keyword."1

r2. The glossary lists this kind first: "positional-or-keyword: specifies an argument that can be passed either positionally or as a keyword argument. This is the default kind of parameter".3

r3. In a call, "If there are N positional arguments, they are placed in the first N slots. Next, for each keyword argument, the identifier is used to determine the corresponding slot".2 Example: mixed.

r4. "If the slot is already filled, a TypeError exception is raised."2 Example: both-ways.

r5. "If there are any unfilled slots for which no default value is specified, a TypeError exception is raised."2 Example: missing.

Examples

def box(width, height, depth=1):
    return width * height * depth

print(box(2, 3))
print(box(2, height=3, depth=4))
print(box(depth=4, height=3, width=2))
Output
6
24
24

The positional arguments fill the first slots, and the keyword arguments fill the slots with their names (rule r3).

Pitfalls

An argument by position and again by name fills one slot twice:

This code raises TypeError, on purpose.

def box(width, height):
    return width * height

box(2, width=3)

A slot with no argument and no default is a TypeError:

This code raises TypeError, on purpose.

def box(width, height):
    return width * height

box(width=2)

The name of a positional-or-keyword parameter is part of the interface of the function, because callers can use it. If you rename the parameter, calls that use the old name fail. The tutorial says: "For an API, use positional-only to prevent breaking API changes if the parameter’s name is modified in the future."1

Comparison

A Go call passes each argument by position. In Go, "the arguments of the call are passed to the function, which means that they are assigned to their corresponding function parameters".4 Go has no call by name, so the name of a Go parameter is not part of what a caller writes. In Python, a caller can use the name of a positional-or-keyword parameter (rule r1).

References

  1. The Python Tutorial, 4. More Control Flow Tools, 4.9.3.1. Positional-or-Keyword Arguments; 4.9.3.4. Function Examples; 4.9.3.5. Recap
  2. The Python Language Reference, Python 3.15, 6.3.3. Calls
  3. Glossary, Python 3.15, parameter
  4. The Go Programming Language Specification, Calls

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