I'm trying to understand this question and answers:
python function default parameter is evaluated only once?
in order to understand it i try:
def f1(a, L=[]):
if not L:
print "L is empty"
L = []
L.append(a)
return L
>>>f1(1)
L is empty
[1]
>>>f1(1)
L is empty
[1]
def f2(a, L=[]):
if L:
print "L isn't empty"
L = []
L.append(a)
return L
>>>f2(1)
[1]
>>>f2(1)
L isn't empty
[1]
So I think in the case of f1
L
is becoming empty again every time - it is assigned to []
again after every call of f1
. But in case of f2
L
is somehow isn't empty? Why?