P
Paul Sweeney
The python tutorial gives the following example to demonstrate the fact
that default args are only evaluated once:
def f(a,L=[]):
L.append(a)
return L
print f(1),f(2),f(3)
[1] [1,2] [1,2,3]
now I'm confident I understand this, but I do not understand how changing
to the following (whatever the merits of so doing, it was an accidental
typo)
results in the output displayed:
def f(a,L=[]):
if not L: L=[]
L.append(a)
return L
[1] [2] [3]
surely on second entry to f, L == [1], so the "if not L:" should not fire?!
I'm running v2.3.3 and have tried this on RH9.0 and W2K (just in case...)
any enlightenment gratefully recieved
THX
that default args are only evaluated once:
def f(a,L=[]):
L.append(a)
return L
print f(1),f(2),f(3)
[1] [1,2] [1,2,3]
now I'm confident I understand this, but I do not understand how changing
to the following (whatever the merits of so doing, it was an accidental
typo)
results in the output displayed:
def f(a,L=[]):
if not L: L=[]
L.append(a)
return L
[1] [2] [3]
surely on second entry to f, L == [1], so the "if not L:" should not fire?!
I'm running v2.3.3 and have tried this on RH9.0 and W2K (just in case...)
any enlightenment gratefully recieved
THX