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)
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))
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
- 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
- The Python Language Reference, Python 3.15, 6.3.3. Calls
- Glossary, Python 3.15, parameter
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag KR09AF Edit this page History Report an error Markdown