I was much surprised by this
>>> def f(arg=['local variable']):
... arg.append('!')
... print arg
...
>>> f()
['local variable', '!']
>>> f()
['local variable', '!', '!']
>>> f()
['local variable', '!', '!', '!']
I'm sure this is by design, but WTF?!? And how should I flush the local namespace, so I'm not surprised in the future?
[EDIT]:
Thank you for the explanation about why this happen. But on a practical level, what is the right way of dealing with it? Just deepcopy all default variables before handling them, like below?
>>> from copy import deepcopy
>>> def f(arg=['local variable']):
... arg = deepcopy(arg)
... arg.append('!')
... print arg
...
>>> f()
['local variable', '!']
>>> f()
['local variable', '!']
>>> f()
['local variable', '!']