# Python Default Arguments: evaluated once

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-default-arguments-full-en
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.

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.

(Test: 3 tests were run, TE-75-A, TE-75-B and TE-75-C. The expected non-zero exit is TE-75-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes 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`):

```bash
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:

```bash
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`):

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

Command:

```bash
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`):

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

Command:

```bash
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.

## Source

- https://docs.python.org/3/tutorial/controlflow.html#default-argument-values

## 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.

## Related guides
- [Python Loop Else: runs when the loop did not break](https://kairos-2z6s.onrender.com/res/python-loop-else-full-en)
- [Python Lambda: a small function inline](https://kairos-2z6s.onrender.com/res/python-lambda-full-en)
- [Python dictionaries: lookup, default values and the KeyError](https://kairos-2z6s.onrender.com/res/python-dict-basics-full-en)
- [Python dates: parse and compare](https://kairos-2z6s.onrender.com/res/python-dates-full-en)
- [Python Defaultdict: a dict that fills missing keys](https://kairos-2z6s.onrender.com/res/python-defaultdict-full-en)
