473,806 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Default Argument Inconsistency?

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
5 1221
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
17091
by: Grzegorz Staniak | last post by:
Hello, I'm a newbie Python user, a systems administrator - I've been trying to switch from Perl to Python for administrative tasks - and one thing I cannot understand so far is why I need the special 'self' (or anything esle) argument in class method definitions. I might have missed an explanation in the docs, a quick Google search did not help. Is there somewhere on the web you could direct me to? Thanks,
46
4273
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
5
15399
by: Dave Vandervies | last post by:
If I feed this to g++: -------- int foo(int i=42); int foo(int i=42) { return i; } -------- It says (with -W -Wall -ansi -pedantic):
36
72122
by: spence | last post by:
Hi All How do I make it so that when a user clicks in a search text field, the default entry (in this case "Search") is removed automatically - they are then faced with a blank search box and can type straight away Thanks in advance
4
5851
by: aling | last post by:
What's the rule of default argument of function in C++? I found that the default argument of function could not necessary be a constant value. Is it true? Previously I thought that the default argument of function must be a constant value. Here is my sample code, it's compiled successully in VC7.1. #include<iostream> class Base{ public :
44
2781
by: gregory.petrosyan | last post by:
Hello everybody! I have little problem: class A: def __init__(self, n): self.data = n def f(self, x = ????) print x All I want is to make self.data the default argument for self.f(). (I
20
1520
by: Gernot Frisch | last post by:
Hi, can I somehow do something like this: void foo(int*) {} int main() {
8
284
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
43
3238
by: kenneth | last post by:
Dear all, I have encountered this weird problem. I have a class definition with an __init__ argument 'd' which defaults to {}. This argument is put in the 'self.d' attribute at initialization I create two independent instances of this class; the code is as follows.
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10620
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10372
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.