473,396 Members | 1,971 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,396 software developers and data experts.

Re: Default Argument Question

On Wed, Oct 29, 2008 at 9:14 AM, Paulo J. Matos <po**@ecs.soton.ac.ukwrote:
Hi all,

Going through the tutorial brought up a question. Consider the functions:

def f(a, L=[]):
L.append(a)
return L

print f(3)
print f(9)
print f(7)

def f1(i = 0):
i = i + 1
print i

f1()
f1()
f1()
f1()

Since the f accumulates the values in L, I was expecting to see i
printing 1,2,3,4 but this doesn't happen.
Can someone please explain why and what is going on beneath the veil?
http://effbot.org/zone/default-values.htm explains this FAQ quite
well. Note that the "accumulation" behavior of lists is considered an
aberration and is usually not desired.

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
Cheers,

--
Paulo Jorge Matos - pocmatos @ gmail.com
--
http://mail.python.org/mailman/listinfo/python-list
Oct 29 '08 #1
1 930
Chris Rebert a écrit :
(snip)
Note that the "accumulation" behavior of lists is considered an
aberration
By who ?

Oct 29 '08 #2

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

Similar topics

5
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):
5
by: Cruxic | last post by:
Can a class constructor be an argument default? It works in Borland C++ but not in MinGW The following won't compile in MinGW because of the error "Invalid type 'Thing1' for default"...
2
by: xuatla | last post by:
The following is just a sample code to demostrate my question: ----------- template <typename T> class C { public: friend void f1(double i=2) { std::cout << i; } ; };
4
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...
44
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
1
by: avasilev | last post by:
Hi all, I am upgrading a kind of small logging framework. Until now, the interface for logging messages takes only a format string, and a variable argument list (in a similiar way as printf())....
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
24
by: Steven D'Aprano | last post by:
Sometimes it seems that barely a day goes by without some newbie, or not- so-newbie, getting confused by the behaviour of functions with mutable default arguments. No sooner does one thread...
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
43
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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,...
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...
0
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,...

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.