Memo from P115 of http://www.amazon.co.jp/dp/4873117380
When defining a function in python, you can set a default value for the argument, but the value of the default value is calculated when the function is defined </ b>, not when the function is executed. ..
Therefore, if you define the function as follows, the list is not empty, and the value at the time of the previous call remains after the second time.
In [32]: def buggy(arg, result=[]):
....: result.append(arg)
....: print(result)
....:
In [33]: buggy('a')
['a']
In [34]: buggy('b')
['a', 'b']
As much as possible, it is a point to prevent bugs by specifying immutable default values.
There are various ways to avoid this, but for example, make it explicit that it is the first call as follows.
In [35]: def nonbuggy(arg, result=None):
....: if result is None:
....: result = []
....: result.append(arg)
....: print(result)
....:
In [36]: nonbuggy('a')
['a']
In [37]: nonbuggy('b')
['b']