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

M

Mike

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.
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
 
T

Troy Melhase

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.
 
M

Mike

Thanks, Troy. I never cease to be amazed at what can be discovered by
reading the manual! <self bangs head on wall>

Mike
 
S

Steven D'Aprano

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?
 
P

Paddy

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?

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

- Paddy.
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

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.
 
S

Steven D'Aprano

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.
 
A

Alex Martelli

Steven D'Aprano said:
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
 
T

Tim Leslie

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.

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?

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

It is a check done by pylint

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top