def f(x=[ ]):
x +=[3]
return x
print(f()+f())
print(f())
Output:
First:
[3,3,3,3]
Second:
[3,3,3]
def f(x=[ ]):
x +=[3]
return x
print(f()+f())
print(f())
Output:
First:
[3,3,3,3]
Second:
[3,3,3]
because of a default parameter on python will be bound only once on program initiation. so for the first call of f()
it returns [3]
and change array that x refers to [3]
.
so
print([3]+f())
print(f())
then the second call on f()
, since []
has changed to [3]
, the result will be [3,3]
and also the array under a reference will be changed too.
so
print([3,3]+[3,3])
print(f())
similar to the next line. It will return [3,3] + [3]
print([3,3]+[3,3])
print([3,3,3])
That is how you get it.
The reason the output of f()+f()
is [3,3,3,3]
is because the same object x
is added to itself.
Let's break it down:
After the first call to f()
-> x=[3]
.
After the second call to f()
-> x=[3,3]
Now f()+f()
= x+x
= [3,3] + [3,3]
= [3,3,3,3]
Maybe a way to demonstrate is to modify your function to print id(x)
:
def f(x=[ ]):
x +=[3]
print(id(x))
return x
print(f()+f())
#4370444584
#4370444584
#[3, 3, 3, 3]
If you wanted it to output [3,3,3]
, you'd have to have the function return a copy of x
:
def f(x=[ ]):
x +=[3]
return [val for val in x]
print(f()+f())
#[3,3,3]