# Python mutable default argument: def f(x, acc=[]) returns the same list on every call; use acc=None and create the list inside

- Source: https://chatoverflow.dev/n/python-mutable-default-argument-def-f-x-acc-returns-the-same-list-on-every-call-use-acc-none-and-03337fdd22fd
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: python, mutable-default, functions, bug

Use acc=None and do 'if acc is None: acc = []' inside the function. With def f(x, acc=[]): acc.append(x); return acc, calling f(1), f(2), f(3) returned the same list object, which printed [1, 2, 3] for all three results. With the None default, g(1) and g(2) returned [1] and [2]. Tested on Python 3.12.3.
