Default Argument Inconsistency?

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
 
D

Diez B. Roggisch

Paul said:
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?!

No, its not. As [] is logically false, the

if not L: L = []

rebinds L with a fresh list. So the append is made to that list, not to the
one L is bound to as default argument.
 
P

Peter Otten

Paul said:
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=[]

The first thing in f() is to check if L evaluates to False, i. e. is empty.
If L is empty the L "the object" is left alone, but L "the identifier" is
bound to a new empty list. So
L.append(a)

will never operate on the empty list that was provided as default parameter.
return L
[1] [2] [3]

surely on second entry to f, L == [1], so the "if not L:" should not
fire?!

While the above works, there is one pitfall that will sooner or later bite
you:
.... if not L: L = []
.... L.append(a)
.... return L
....
l1 = ["old"]
f("new", l1) ['old', 'new']
l1 ['old', 'new']
l2 = []
f("new", l2) ['new']
l2 []

Did you predict the values of l1 and l2 correctly? Congratulations.

I recommend that you adopt the common practice and initialize mutable
parameters with None as the default, which gives you consistent behaviour:
.... if L is None: L = []
.... L.append(a)
.... return L
....
l1 = ["old"]
f("new", l1) ['old', 'new']
l1 ['old', 'new']
l2 = []
f("new", l2) ['new']
l2 ['new']

Peter
 
H

Heiko Wundram

I guess I'll best describe this behavior when commenting directly in the code.

Am Dienstag, 27. April 2004 11:24 schrieb Paul Sweeney:
def f(a,L=[]):
if not L: L=[]

It checks whether not L (in this context, meaning that the list is empty). The
list is (initialized with []), so the action is triggered. The action creates
a new empty list, whose reference is now stored in L. The list which is
constructed as the default argument is unbound from L, but still referenced
in the function object for f (as the default argument for L, if it isn't
present).
L.append(a)

The append appends an item to the newly created empty list.

The append returns the new list.

Hope this sheds light on this behavior. When calling in for the second time,
the default argument is still empty, a new list is created, etc. The default
argument thus never changes from being the empty list.

Heiko.
 
P

Paul Sweeney

Ah, yes, got it :)

I'm new to Python (4 months) and thought I'd figured the whole
immutable/mutable thang,
and this was bothering me.

Many thanks

Paul



"Diez B. Roggisch" wrote ...
 
P

Paul Sweeney

That's a great gotcha!

The typo I knew about was that I should have had
def f(a, L=None):

but as you point out also the 'if' should be more explicitly
if L is None: L = []

As I said in reply to Diez, I'm new to Python and had switched
in general from my initial style of

if L is None or L==[]:
to
if not L:

which was clearly wrong here. As always, "a little knowledge
is a dangerous thing" :)


Thanks, (to me ;-) an interesting problem and explanations

Paul


Peter Otten said:
... there is one pitfall that will sooner or later bite
you:
... if not L: L = []
... L.append(a)
... return L
...
l1 = ["old"]
f("new", l1) ['old', 'new']
l1 ['old', 'new']
l2 = []
f("new", l2) ['new']
l2 []

Did you predict the values of l1 and l2 correctly? Congratulations.
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top