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 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.
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.
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.
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.
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
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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);
|
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
|
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.
| |
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)
{
|
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)();
|
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
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |