def X(l=[]): weirdness. Python bug ?

B

Bart van Deenen

Hi all.

I've stumbled onto a python behavior that I don't understand at all.

Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)

# function
def X(l=[]):
l.append(1)
print l

# first call of X
X()
[1]

#second call of X
X()
[1, 1]

Where does the list parameter 'l' live between the two successive calls of X().
Why is it not recreated with an empty list?
Is this correct behavior or is it a Python bug?
Does anyone have any pointers to the language documentation where this behavior is described?

Thanks all

Bart van Deenen
 
C

cokofreedom

Hi all.

I've stumbled onto a python behavior that I don't understand at all.

Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)

# function
def X(l=[]):
   l.append(1)
   print l

# first call of X
X()
[1]

#second call of X
X()
[1, 1]

Where does the list parameter 'l' live between the two successive calls of X().
Why is it not recreated with an empty list?
Is this correct behavior or is it a Python bug?
Does anyone have any pointers to the language documentation where this behavior is described?

Thanks all

Bart van Deenen

http://docs.python.org/ref/function.html

"Default parameter values are evaluated when the function definition
is executed."

Depending on your use the common way to handle this is to do

def x(lst = None):
if lst is None:
pass # lst has not been set to anything
else:
pass # lst has been set to something
 
B

Bart van Deenen

Hi

Thanks all for your answers. I figured your solution already, but now I understand where the behavior is from. One question remains: can I find my parameter 'l' somewhere? I looked in a lot of objects, but couldn't find it.

Thanks

Bart.

# function
def X(l=[]):
l.append(1)
print l

# first call of X
X()
[1]

#second call of X
X()
[1, 1]

Where does the list parameter 'l' live between the two successive calls
of X(). Why is it not recreated with an empty list?
Is this correct behavior or is it a Python bug?
Does anyone have any pointers to the language documentation where this
behavior is described?

"Default parameter values are evaluated when the function definition
is executed."

Depending on your use the common way to handle this is to do

def x(lst = None):
if lst is None:
pass # lst has not been set to anything
else:
pass # lst has been set to something
 
D

Diez B. Roggisch

B

Bruno Desthuilliers

Bart van Deenen a écrit :
(ot : please don't top post - corrected)
# function def X(l=[]): l.append(1) print l

# first call of X X() [1]

#second call of X X() [1, 1]

Where does the list parameter 'l' live between the two successive
calls of X(). Why is it not recreated with an empty list? Is this
correct behavior or is it a Python bug? Does anyone have any
pointers to the language documentation where this behavior is
described?
"Default parameter values are evaluated when the function
definition is executed."
(snip)
> Thanks all for your answers. I figured your solution already, but now
> I understand where the behavior is from. One question remains: can I
> find my parameter 'l' somewhere? I looked in a lot of objects, but
> couldn't find it.

def foo(x="default value for x"):
pass

print foo.func_defaults
=> ('default value for x',)
 
B

Bart van Deenen

Diez said:
It's amazing. I didn't analyse this properly, but IMHO this issue is the
single most asked question (or rather the effects in produces) on this
list.
I feel a bit dumb to ask a FAQ on the newsgroup. The problem with this particular question is that I found it hard to find a query that would give meaningful answers.

Thanks for your patience all.

Bart
 
T

Terry Reedy

Bart said:
I feel a bit dumb to ask a FAQ on the newsgroup. The problem with
this particular question is that I found it hard to find a query that
would give meaningful answers.

See my new thread "How to search the Python manuals".

tjr
 
A

Andrew Lee

Bart said:
Hi all.

I've stumbled onto a python behavior that I don't understand at all.

Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)

# function
def X(l=[]):
l.append(1)
print l

# first call of X
X()
[1]

#second call of X
X()
[1, 1]

Where does the list parameter 'l' live between the two successive calls of X().
Why is it not recreated with an empty list?
Is this correct behavior or is it a Python bug?
Does anyone have any pointers to the language documentation where this behavior is described?

Thanks all

Bart van Deenen

I happen to be reading about decorators at the moment:

from copy import deepcopy
def nodefault(myfunc):
myfunc_defaults = myfunc.func_defaults
def fresh(*args, **kwargs):
myfunc.func_defaults = deepcopy(myfunc_defaults)
return myfunc(*args, **kwargs)
return fresh

@nodefault
def X(l=[]):
l.append(1)
print l
.... X()
....
[1]
[1]
[1]
[1]
[1]


Which is just a very fancy way of doing:
def X(l=[]):
if l is None:
l = []
l.append(1)
print l

* sound of two pennies *
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top