KKAIROS
Practical guideIllustrative3 min read

Python Default Arguments: evaluated once

A default value is evaluated once, when the function is defined. A mutable default such as a list is shared between calls. Use None and create the list inside the function.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-75-A: a list default is shared between calls, a None default is not

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
def bad(x, acc=[]):
    acc.append(x)
    return acc
def good(x, acc=None):
    if acc is None:
        acc = []
    acc.append(x)
    return acc
print(bad(1))
print(bad(2))
print(good(1))
print(good(2))
EOS

Command:

python3 s.py

Recorded output:

[1]
[1, 2]
[1]
[2]

Exit status: 0. The output matches the expected text written before the run.

TE-75-B: a default value is computed once, when the def line runs, and is stored on the function

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
def f(x, n=len([1, 2])):
    return n
print(f(0))
print(f.__defaults__)
EOS

Command:

python3 s.py

Recorded output:

2
(2,)

Exit status: 0. This status and the output match what was written before the run.

TE-75-C: expected non-zero exit: a parameter without a default after one with a default is a SyntaxError

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
def f(a=1, b):
    pass
EOS

Command:

python3 s.py

Recorded output: none, standard output was empty.

Standard error from the same run:

File "/tmp/b21-b05ec42k/work/s.py", line 1
    def f(a=1, b):
               ^
SyntaxError: non-default argument follows default argument

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

TE-75-B: the default of n is the expression len([1, 2]). f(0) printed 2, and f.__defaults__ printed (2,), the value stored on the function. TE-75-C: Python stopped with exit status 1 before running any line. The last line of standard error is SyntaxError: non-default argument follows default argument.

Limits

TE-75-A shows a list default, and TE-75-B shows a default computed from an expression. Other mutable types behave the same way but were not tested.

Ad

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-75-A | a list default is shared between calls, a None default is not | 0 | PASS | fd43089375f2 | | TE-75-B | a default value is computed once, when the def line runs, and is stored on the function | 0 | PASS | 9872206b0edc | | TE-75-C | expected non-zero exit: a parameter without a default after one with a default is a SyntaxError | 1 | PASS | 440539b57417 |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides