By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,813 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,813 IT Pros & Developers. It's quick & easy.

Default Argument Inconsistency?

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Paul Sweeney wrote:
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?!


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.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
Paul Sweeney wrote:
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
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?!


While the above works, there is one pitfall that will sooner or later bite
you:
def f(a, L=[]): .... 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:
def f(a, L=None): .... 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
Jul 18 '05 #3

P: n/a
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.
return L


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.

Jul 18 '05 #4

P: n/a
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 ...

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.
--

Jul 18 '05 #5

P: n/a
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" <__*******@web.de> wrote...
... there is one pitfall that will sooner or later bite
you:
def f(a, L=[]): ... 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.
...

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.