A function keeps remembering the list from the previous call
Asked by Inès Berger · 643 views · 2 replies
This is driving me slightly mad:
def collect(item, into=[]):
into.append(item)
return into
print(collect("a")) # ['a']
print(collect("b")) # ['a', 'b'] <- why?
I expected a fresh empty list each time. Instead the second call sees the first call's data. There is no global anywhere.