Check out the following code:
def f(x, myList = []):
myList.append(x)
return myList
f(6)
returns [6]
while
f(7)
returns [6, 7]
My question is why it doesn't use the default myList value when no value is specified.
On the other hand, this code works fine
def f(x, myList = None):
if myList == None:
# This WILL allocate a new list on every call to the function.
myList = []
myList.append(x)
return myList
f(6)
returns [6]
f(7)
returns [7]
Why in the later case it takes the default argument value but not in the former case?