ML Foundations
Stop a Python default list from leaking history between calls
Create a fresh list for each independent call while allowing intentional shared state.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
A default list is created once
Python evaluates default parameter expressions when a function definition executes. The resulting object is reused when later calls omit that argument. If that object is a list and the function appends to it, the next call sees the earlier mutation. [1, 2]
This can leak history between independent training and evaluation steps. The issue is not that lists cannot be passed to functions; the question is who owns the list and whether reuse was intended.
Use a sentinel for independent calls
Run python examples/python_mutable_default.py. The first helper uses a list as its default. Its first result contains train, while its second contains both train and test, even though the caller supplied no history in either call. Copies are returned so the printed first result stays a snapshot. [1]
The second helper defaults history to None and creates a new list inside the function whenever the argument is omitted. Independent train and test calls then produce separate lists. The example also passes one explicit list twice to demonstrate intentional sharing, and assertions check both behaviors.
Use an identity check against None, not a truthiness check: an explicitly supplied empty list is a valid object that the caller may want the function to update. This sentinel pattern is documented in the Python tutorial. [1]
Executed with Python 3.12.14; stdout and checksum are in private evidence.
def collect_bad(value, history=[]):
history.append(value)
return history.copy()
def collect_good(value, history=None):
if history is None:
history = []
history.append(value)
return history.copy()
print('bad first:', collect_bad('train'))
print('bad second:', collect_bad('test'))
print('good first:', collect_good('train'))
print('good second:', collect_good('test'))
shared = []
collect_good('train', shared)
collect_good('test', shared)
print('explicit shared:', shared)
assert collect_good('new') == ['new']
assert shared == ['train', 'test']
Recorded output
bad first: ['train'] bad second: ['train', 'test'] good first: ['train'] good second: ['test'] explicit shared: ['train', 'test']
Make the ownership contract visible
If a caller passes a list, this helper appends to that list even though it returns a copy. A production API should document that mutation, or make a copy before appending if callers require their input unchanged. A fresh default prevents accidental cross-call sharing; it does not prevent deliberately passed objects from being shared.
The fixture uses two strings and no concurrent calls. It does not model a complete training pipeline, thread safety or performance. Mutable defaults can also be used deliberately for persistent state, but that behavior should be explicit to readers and callers.