473,583 Members | 3,140 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 1212
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
17054
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...
46
4201
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
15384
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
72041
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
5835
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
2743
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
1494
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
3194
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
8186
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. ...
0
8329
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7936
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...
0
8197
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5704
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...
0
5377
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...
0
3821
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...
1
2335
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
0
1160
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.