I have reduced to this simple code an unexpected behavior in a larger python code:
def f(n, s=[]):
s.append(n)
return s
print(f(3))
print(f(5))
The output given is:
[3]
[3, 5]
I expected:
[3]
[5]
If the empty list is given explictly as argument, f(3, s=[]), it works as expected. I imagine that this is a bad practice, has this happened to anyone?
(The function looks useless but the original had some random variables sampled inside)
New contributor
cadrete is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.