What is the output?
def f(n, acc=[]):
acc.append(n)
return acc
f(1)
print(f(2))
- [1, 2]
- [2]
- [1]
- [[1], [2]]
The default list is created once, so it persists between calls.
Play Problem Solving ▸
def f(n, acc=[]):
acc.append(n)
return acc
f(1)
print(f(2))