I have a simple python question. Consider the following code.
import random
def int_list(local_list=[]):
local_list.append(random.randint(0,1000))
return local_list
for i in range(5):
print(int_list())
Under both python2 and python3 the result is:
>> [828]
>> [828, 268]
>> [828, 268, 999]
>> [828, 268, 999, 471]
>> [828, 268, 999, 471, 847]
I would have expected the following result instead:
>> [828]
>> [268]
>> [999]
>> [471]
>> [847]
Apparently the object used to instantiate local_list has global scope and is the same for every invocation. Not only does this surprise me, but it also seems like incorrect or at least undesirable behavior. I assume this question has been raised before. Can someone point me to a rationale?