473,394 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

help explaining default function arg weirdness

This is almost straight out of the Python tutorial,
section 4.7.1

------------------------code ---------------------
#!/usr/bin/python

def func( append_this, default_list=[] ):
default_list.append( append_this )
return default_list

print func( "foo" )
print func( "bar" )
print func( "baz" )
------------------------/code-----------------

And running it gives me this:

['foo']
['foo', 'bar']
['foo', 'bar', 'baz']

which looks wrong to me. The explanation in the tutorial
says "The default value is evaluated only once. This makes a
difference when the default is a mutable object such as a
list, dictionary, or instances of most classes."

I don't get it: isn't default_list a local to func()?
Doesn't it get created/destroyed with each call to func()
so we'd get a fresh empty one with each function call?

What's the rationale for having the function remember
default_list across calls?

Thanks,
---J

--
--- remove zees if contacting via email ---
Jul 18 '05 #1
0 877

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bob | last post by:
Below is sample code that illustrates what I'm trying to do. For sake of brevity I didn't include the properties of buildBtn that determine what data to request. The problem is I never see...
6
by: Andi Plotsky | last post by:
Hi - I sent this request last week, but didn't get an answer to my problem - let's try again: In my A2K database, I have a form with 2 Unbound Comboboxes, both of which open up forms to a...
4
by: gs | last post by:
I have searched Google, MSDN,... for a week. I am still unable to make available functions in my csharp dll as native windows functions for some legacy non dotnet application I just want to...
11
by: dhnriverside | last post by:
Hi peeps Ok, so I thought I'd have a go at making a console app in VS2k5... I haven't written any windows apps for years, let alone dos apps (been web programming) and I've hit a dumb error... ...
3
by: Marty | last post by:
Hi, I have this DLL made with VB.NET made as a Class Library project. I know this is managed code. I have my other made with VC++.NET, unmanaged code. The point is that I want to use...
2
by: jm | last post by:
I am trying to do something VERY simple. I want to use this: http://www.codeproject.com/csharp/dotnetbandobjects.asp?msg=727553#xx714692xx to point to a web page. I cannot use process.start...
5
by: David Thielen | last post by:
Hi; I am creating png files in my ASP .NET app. When I am running under Windows 2003/IIS 6, the file is not given the security permissions it should have. It does not have any permission for...
3
by: mhvaughn | last post by:
struct S1 { int i; }; struct S2 { S1 s; // version 1 S2() {} ; // version 2
2
by: JYA | last post by:
Hi. I was writing an xmltv parser using python when I faced some weirdness that I couldn't explain. What I'm doing, is read an xml file, create another dom object and copy the element from...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.