473,834 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function with list argument defaulting to [] - what's going on here???

While trying to write a recursive function involving lists, I came
across some (to me) odd behavior which I don't quite understand. Here's
a trivial function showing the problem.
>>def f(l, r = []):
for itm in l:
r.append(itm)
print r

>>a = [1,2,3]
f(a)
[1, 2, 3]
>>f(a)
[1, 2, 3, 1, 2, 3]
>>f(a)
[1, 2, 3, 1, 2, 3, 1, 2, 3]

I know the function is quite artificial, but it's for illustration only.
Why is "r" not being reset to the empty list on subsequent calls? It
seems like it should be reinitialized when not explicitly provided.

Thanks in advance.
Mike
Apr 15 '07 #1
9 1390
On 4/14/07, Mike <mm***@woh.rr.c omwrote:
While trying to write a recursive function involving lists, I came
across some (to me) odd behavior which I don't quite understand. Here's
a trivial function showing the problem.
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.
Apr 15 '07 #2
Thanks, Troy. I never cease to be amazed at what can be discovered by
reading the manual! <self bangs head on wall>

Mike

Troy Melhase wrote:
On 4/14/07, Mike <mm***@woh.rr.c omwrote:
>While trying to write a recursive function involving lists, I came
across some (to me) odd behavior which I don't quite understand. Here's
a trivial function showing the problem.

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.
Apr 15 '07 #3
On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:
On 4/14/07, Mike <mm***@woh.rr.c omwrote:
>While trying to write a recursive function involving lists, I came
across some (to me) odd behavior which I don't quite understand. Here's
a trivial function showing the problem.

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.


This comes up so often that I wonder whether Python should issue a warning
when it sees [] or {} as a default argument.
What do people think? A misuse or good use of warnings?

--
Steven.

Apr 15 '07 #4
Mike <mm***@woh.rr.c omwrote:
...
Why is "r" not being reset to the empty list on subsequent calls? It
seems like it should be reinitialized when not explicitly provided.
<http://www.python.org/doc/faq/genera...lues-shared-be
tween-objects>
Alex
Apr 15 '07 #5
On Apr 15, 3:58 am, Steven D'Aprano
<s...@REMOVE.TH IS.cybersource. com.auwrote:
On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:
On 4/14/07, Mike <m...@woh.rr.co mwrote:
While trying to write a recursive function involving lists, I came
across some (to me) odd behavior which I don't quite understand. Here's
a trivial function showing the problem.
fromhttp://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.

This comes up so often that I wonder whether Python should issue a warning
when it sees [] or {} as a default argument.

What do people think? A misuse or good use of warnings?

--
Steven.
I wonder if it is a check done by Pylint or PyChecker?

- Paddy.

Apr 15 '07 #6
This comes up so often that I wonder whether Python should issue a warning
when it sees [] or {} as a default argument.
What do people think? A misuse or good use of warnings?
I think Python should reevaluate the default values.

--
mvh Björn
Apr 15 '07 #7
On Sun, 15 Apr 2007 05:29:01 +0200, BJörn Lindqvist wrote:
>This comes up so often that I wonder whether Python should issue a warning
when it sees [] or {} as a default argument.
What do people think? A misuse or good use of warnings?

I think Python should reevaluate the default values.
That would break code that relies on the current behaviour. That makes it
a "maybe" for Python 3.0, and an absolute "NO!!!" for Python 2.x.

--
Steven.
Apr 15 '07 #8
Steven D'Aprano <st***@REMOVE.T HIS.cybersource .com.auwrote:
On Sun, 15 Apr 2007 05:29:01 +0200, BJörn Lindqvist wrote:
This comes up so often that I wonder whether Python should issue a warning
when it sees [] or {} as a default argument.
What do people think? A misuse or good use of warnings?
I think Python should reevaluate the default values.

That would break code that relies on the current behaviour. That makes it
a "maybe" for Python 3.0, and an absolute "NO!!!" for Python 2.x.
If you hope to get any change in Python 3.0, your PEP had better be in
before the end of April -- that's the 3.0 deadline for PEPs.
Alex
Apr 15 '07 #9
On 14 Apr 2007 20:20:42 -0700, Paddy <pa*******@goog lemail.comwrote :
On Apr 15, 3:58 am, Steven D'Aprano
<s...@REMOVE.TH IS.cybersource. com.auwrote:
On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:
On 4/14/07, Mike <m...@woh.rr.co mwrote:
>While trying to write a recursive function involving lists, I came
>across some (to me) odd behavior which I don't quite understand. Here's
>a trivial function showing the problem.
fromhttp://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.
This comes up so often that I wonder whether Python should issue a warning
when it sees [] or {} as a default argument.

What do people think? A misuse or good use of warnings?

--
Steven.

I wonder if it is a check done by Pylint or PyChecker?
It is a check done by pylint

Tim
>
- Paddy.

--
http://mail.python.org/mailman/listinfo/python-list
Apr 15 '07 #10

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

Similar topics

9
4969
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or anything...it just returns a "1" (whereas it should return a "0") as far as I can tell. I have read the PHP...
7
6302
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments passed to the function are strings or must be (automaticly converted to a string e.g. the number 10 should become the string "10". My problem is that I can only find samples and description of printf() like functions where the optional arguments and...
8
3406
by: BekTek | last post by:
I have class that has member function that take two argument.. I'd like to use the member function to algorithm such as remove_if.. How can I bind that? class Foo{ void f(){ int a = 10; remove_if(list.begin(), list.end(), HERE);
17
2239
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have an algorithm that modifies this datastructure. The basic outline of the algorithm is independent of the type of property. So I implemented a generic version of the algorithm and a function object for
50
3347
by: LaundroMat | last post by:
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to pass a value to f()? "None" doesn't seem to work.. Thanks in advance.
28
4349
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
6
2215
by: Charles Sullivan | last post by:
I define and initialize an array of structures like the following, (where the <verbiage within angle bracketsis just meant to be explanatory): int func1(<argument prototypes>); int func2(<argument prototypes>); struct mystruct { <other stuff>; int (*myfunc)();
28
4721
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
8
5542
by: Jon Harrop | last post by:
I am trying to learn C# and .NET programming in general but I am finding it very hard going. To start with, I'd like to translate some trivial functions from other languages that I am familiar with into C#. Here is a simple function in OCaml that nests "n" applications of "f" around "x", with "n" defaulting to "n=2": let rec nest ?(n=2) f x = if n=0 then x else nest ~n:(n-1) f (f x) For example, "nest f x" gives "f(f(x))" and "nest...
0
9796
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
9643
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
10786
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...
0
10503
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7754
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
6951
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();...
1
4425
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
2
3973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3079
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.