The behavior of the default argument of python seems to be misunderstood and scary, so be careful.
➜ ~ python
Python 2.7.11 (default, Feb 2 2016, 21:44:54)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def func(arg={}):
... return arg
...
>>> foo = func()
>>> foo['a'] = 'foo'
>>>
>>> bar = func()
>>> bar['b'] = 'bar'
>>> print(foo)
{'a': 'foo', 'b': 'bar'}
>>> print(bar)
{'a': 'foo', 'b': 'bar'}
The default arguments are shared between calls because they are evaluated only once when the module is loaded. I wonder why I made this specification ...
Recommended Posts