I am following the Python tutorial.
def f(a, L=[]):
L.append(a)
return L
When I call f
print(f(1))
print(f(2))
print(f(3, L=[]))
print(f(4))
I get
[1]
[1, 2]
[3]
[1, 2, 4]
Why it returns [1, 2, 4] after [3] instead of [3, 4]?
The problem is that you declare L=[] in the arguments. This creates the List that is assigned to L only once. For the third case where you have
print(f(3, L=[]))
you pass an empty list to the function for the parameter L. This does not overwrite L but is only valid for that specific function call. For the next call, it takes the default argument list again, which contains at that point in time [1,2].
It is (as far as I know) not possible to reassign the default parameter after initialization.
This question may also be of interest to you as it talks about mutable default arguments
If you want to get the same result you need to initialize a list and pass it to the function.
def f(a, L):
L.append(a)
return L
L = []
print(f(1, L))
print(f(2, L))
L = []
print(f(3, L))
print(f(4, L))
The output is
[1]
[1, 2]
[3]
[3, 4]
You're getting a different value because of L=[] default value and it is mutable. The tutorial link you provided there clearly stated that
So, When you pass another L=[] in your f() function then L=[] is initiating for a new one and return a single result. But after calling f(4) it's getting to default function and return update result
def f(a, L=[]):
L.append(a)
return L
print(f(1))
print(f(2))
print(f(3, L=[]))
print(f(4))
Looks like that you are creating a new list object in memory by resetting a new value object of L at the third time it runs, so it stores the the a variable value in the new list object instead of appending to the old one that has been in memory already, but in step four and since L=[] has hasn't been changed this time it still points to the same old list object in memory, so step 4 a variable value gets stored there as well as step one and two.
here is a simple example:
if we modified the code a little bit to debug what's happening here, you will see that the L variable get's assigned to a new list object in memory at step 3.
def f(a, L=[]):
L.append(a)
print(f'Your function list id: {id(L)}, now gets assigned a value of: {a}')
return L
print(f(1))
print(f(2))
print(f(3, L=[]))
print(f(4))