Default Argument Inconsistency?
Paul Sweeney
reverse.ku.oc.issolok at nothypgnal.delrest.co.uk
Tue Apr 27 05:24:44 EDT 2004
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
>>> print f(1),f(2),f(3)
[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
More information about the Python-list
mailing list