Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
This is very odd, an optional list parameter in Python is persistent between function calls when using the .append() method.
def wtf(some, thing, fields=[]):
print fields
if len(fields) == 0:
fields.append('hey');
print some, thing, fields
wtf('some', 'thing')
wtf('some', 'thing')
The output:
[]
some thing ['hey']
['hey'] # This should not happen unless the fields value was kept
some thing ['hey']
Why does the "fields" list contain "hey" when it is a parameter? I know it's local scope because I can't access it outside the function, yet the function remembers its value.