Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
def stackdemo(stack=[]):
stack.append('q')
return stack
stackdemo()
print stackdemo()
returns ['q','q']
, whereas
stackdemo([])
print stackdemo([])
with the same function returns just ['q']
, as expected.
Why does Python appear to reuse the array if the default is used? Am I missing something?