473,398 Members | 2,188 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,398 software developers and data experts.

default method parameter behavior

I ran into a similar situation like the following (ipython session).
Can anyone please explain why the behavior?
Thanks in advance.

In [11]: def foo(b=[]):
....: b.append(3)
....: return b
....:

In [12]: foo()
Out[12]: [3]

In [13]: foo()
Out[13]: [3, 3]

In [14]: foo([])
Out[14]: [3]

In [15]: foo([])
Out[15]: [3]
Apr 2 '08 #1
2 1268
On Wed, Apr 2, 2008 at 10:59 PM, <ji***********@gmail.comwrote:
I ran into a similar situation like the following (ipython session).
Can anyone please explain why the behavior?
Of course.
>From http://docs.python.org/ref/function.html:
Default parameter values are evaluated when the function definition is
executed. This means that the expression is evaluated once, when the
function is defined, and that that same ``pre-computed'' value is used
for each call. This is especially important to understand when a
default parameter is a mutable object, such as a list or a dictionary:
if the function modifies the object (e.g. by appending an item to a
list), the default value is in effect modified.

--
kv
Apr 2 '08 #2
On Wed, Apr 2, 2008 at 3:59 PM, <ji***********@gmail.comwrote:
I ran into a similar situation like the following (ipython session).
Can anyone please explain why the behavior?
http://www.python.org/doc/faq/genera...etween-objects

Since you got bitten by this, you may also find it useful to take a
look at one of the pages that talks about common python problems, like
http://zephyrfalcon.org/labs/python_pitfalls.html or
http://www.ferg.org/projects/python_gotchas.html (both of which
mention the problems with mutable default arguments).

--
Jerry
Apr 2 '08 #3

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

Similar topics

18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
0
by: silesius | last post by:
I've been using VS 2003 to develop a webapplication using C#. Today I exported the application to a remote webserver I begun experiencing problems. It's a simple application that retrieves some...
4
by: indigator | last post by:
I have an ASP.Net web service class, DataLayer.asmx.cs. I have two constructors for the DataLayer class. One is the default parameter-less one and the second one accepts a string argument. When I...
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
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
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...
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.