Featureintro
Default argument values
Also called default parameter value, optional argument, mutable default argument, default evaluated once, None as default, def f(x=1).
A parameter written name=expression has a default value, so a call can omit its argument. Python evaluates the default once, when the def statement runs, and each call shares that same object.
- Language
- Python
Synopsis
def greet(name, greeting="Hello", mark="!"):
return f"{greeting}, {name}{mark}"
print(greet("Ann"))
print(greet("Bob", "Hi"))
print(greet("Cy", mark="?"))
Hello, Ann!
Hi, Bob!
Hello, Cy?
greeting and mark have default values. A call gives only the arguments that it needs.
Semantics
r1. "For a parameter with a default value, the corresponding argument may be omitted from a call, in which case the parameter’s default value is substituted."2 Example: greet.
r2. "If a parameter has a default value, all following parameters up until the “*” must also have a default value".2 Example: order.
r3. "Default parameter values are evaluated from left to right when the function definition is executed. This means that the expression is evaluated once, when the function is defined, and that the same “pre-computed” value is used for each call."2 Example: evaluated-at-def.
r4. "The default values are evaluated at the point of function definition in the defining scope".1 Example: evaluated-at-def.
r5. "The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes."1 Example: shared-list.
r6. "A way around this is to use None as the default, and explicitly test for it in the body of the function".2 Example: none-default.
Examples
i = 5
def f(arg=i):
return arg
i = 6
print(f())
5
This is the example of the tutorial. The default is the value of i when def runs (rules r3 and r4).
def append_to(item, items=None):
if items is None:
items = []
items.append(item)
return items
print(append_to(1))
print(append_to(2))
[1]
[2]
Each call makes a new list (rule r6).
def fib(n, *, _memo={0: 0, 1: 1}):
if n not in _memo:
_memo[n] = fib(n - 1) + fib(n - 2)
return _memo[n]
print(fib(30))
832040
The FAQ says that this shared object can be useful: "This feature can be useful. When you have a function that’s time-consuming to compute, a common technique is to cache the parameters and the resulting value of each call to the function".3
Pitfalls
A mutable default keeps its changes from one call to the next:
This code runs but gives a wrong answer on purpose.
def append_to(item, items=[]):
items.append(item)
return items
print(append_to(1))
print(append_to(2))
[1]
[1, 2]
The FAQ says: "Default values are created exactly once, when the function is defined."3 So each call that omits items uses the same list. Use None as the default (rule r6).
A parameter with no default cannot follow one with a default:
This code raises SyntaxError, on purpose.
def f(a=1, b):
return a + b
A default such as when=time.time() is also evaluated once, so each call gets the same time (rule r3). Use None and compute the value in the body (rule r6).
Comparison
Go has no default parameter values. In Go, "A function type denotes the set of all functions with the same parameter and result types."4 So each call gives a value for each parameter. The nearest Go form is a final variadic parameter: "A function with such a parameter is called variadic and may be invoked with zero or more arguments for that parameter."4 In Python, a default is part of the def statement, and Python evaluates it once (rule r3).
References
- The Python Tutorial, 4. More Control Flow Tools, 4.9.1. Default Argument Values
- The Python Language Reference, Python 3.15, 8.7. Function definitions
- Programming FAQ, Python 3.15, Why are default values shared between objects?
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag V5VE1M Edit this page History Report an error Markdown