Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > function with list argument defaulting to [] - what's going on here???

Reply
Thread Tools

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

 
 
Mike
Guest
Posts: n/a
 
      04-15-2007
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
 
Reply With Quote
 
 
 
 
Troy Melhase
Guest
Posts: n/a
 
      04-15-2007
On 4/14/07, Mike <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Mike
Guest
Posts: n/a
 
      04-15-2007
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 <(E-Mail Removed)> wrote:
>> 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.

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-15-2007
On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:

> On 4/14/07, Mike <(E-Mail Removed)> wrote:
>> 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.

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      04-15-2007
Mike <(E-Mail Removed)> wrote:
...
> 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
 
Reply With Quote
 
Paddy
Guest
Posts: n/a
 
      04-15-2007
On Apr 15, 3:58 am, Steven D'Aprano
<(E-Mail Removed)> wrote:
> On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:
> > On 4/14/07, Mike <(E-Mail Removed)> wrote:
> >> 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.

 
Reply With Quote
 
=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=
Guest
Posts: n/a
 
      04-15-2007
> 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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-15-2007
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.


 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      04-15-2007
Steven D'Aprano <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
Tim Leslie
Guest
Posts: n/a
 
      04-15-2007
On 14 Apr 2007 20:20:42 -0700, Paddy <(E-Mail Removed)> wrote:
> On Apr 15, 3:58 am, Steven D'Aprano
> <(E-Mail Removed)> wrote:
> > On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:
> > > On 4/14/07, Mike <(E-Mail Removed)> wrote:
> > >> 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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
function argument dependent on another function argument? Reckoner Python 11 01-19-2009 03:31 AM
?? dropdownlist in datagrid, not defaulting to current value ??? THANK YOU jason@cyberpine.com ASP .Net 2 02-16-2005 03:10 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM
defaulting argument to previous argument Bhushit Joshipura C++ 5 12-30-2003 03:21 PM



Advertisments