Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

1 view
Skip to first unread message

Bart van Deenen

unread,
Aug 22, 2008, 5:13:52 AM8/22/08
to
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

cokof...@gmail.com

unread,
Aug 22, 2008, 5:24:20 AM8/22/08
to
On Aug 22, 11:13 am, 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

Wojtek Walczak

unread,
Aug 22, 2008, 5:27:01 AM8/22/08
to
On Fri, 22 Aug 2008 11:13:52 +0200, Bart van Deenen wrote:

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

...


> Does anyone have any pointers to the language documentation where this behavior is described?

Yes, it's documented in FAQ:
http://www.python.org/doc/faq/general/
Question 4.22.

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/

Bart van Deenen

unread,
Aug 22, 2008, 5:41:18 AM8/22/08
to
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.

cokof...@gmail.com wrote:

> On Aug 22, 11:13 am, Bart van Deenen
> <b...@at.vandeenensupport.punt.com.invalid> wrote:
>>
>> # 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?
>>

Diez B. Roggisch

unread,
Aug 22, 2008, 5:42:03 AM8/22/08
to
Wojtek Walczak schrieb:

> On Fri, 22 Aug 2008 11:13:52 +0200, Bart van Deenen wrote:
>
>> I've stumbled onto a python behavior that I don't understand at all.
> ...
>> Does anyone have any pointers to the language documentation where this behavior is described?
>
> Yes, it's documented in FAQ:
> http://www.python.org/doc/faq/general/
> Question 4.22.
>

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.

Maybe we should get *really* explicit in

http://docs.python.org/tut/node6.html#SECTION006710000000000000000

and

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

Big, red warnings, or some such.

Diez

Bruno Desthuilliers

unread,
Aug 22, 2008, 6:04:20 AM8/22/08
to
Bart van Deenen a écrit :
(ot : please don't top post - corrected)

> cokof...@gmail.com wrote:
>
>> On Aug 22, 11:13 am, Bart van Deenen
>> <b...@at.vandeenensupport.punt.com.invalid> wrote:
>>> # 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',)


Wojtek Walczak

unread,
Aug 22, 2008, 6:05:05 AM8/22/08
to
On Fri, 22 Aug 2008 11:41:18 +0200, Bart van Deenen wrote:

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

YOURFUNCTION.func_globals['YOURFUNCTION'].func_defaults

Wojtek Walczak

unread,
Aug 22, 2008, 6:05:06 AM8/22/08
to
On Fri, 22 Aug 2008 11:42:03 +0200, Diez B. Roggisch wrote:

> 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.
>
> Maybe we should get *really* explicit in
>
> http://docs.python.org/tut/node6.html#SECTION006710000000000000000
>
> and
>
> http://docs.python.org/ref/function.html
>
> Big, red warnings, or some such.

+1

Documenting this should also make it easier to understand
the difference between mutable/immutable objects before one
comes on c.l.py and asks about it.

Bart van Deenen

unread,
Aug 22, 2008, 12:06:42 PM8/22/08
to
Diez B. Roggisch wrote:

> 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

Terry Reedy

unread,
Aug 22, 2008, 4:17:22 PM8/22/08
to pytho...@python.org

Bart van Deenen wrote:

> 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

Fredrik Lundh

unread,
Aug 23, 2008, 6:01:07 AM8/23/08
to pytho...@python.org
Bart van Deenen wrote:

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

http://effbot.org/zone/default-values.htm

> Is this correct behavior or is it a Python bug?

Python's been out there for nearly 20 years. I think you safely can
assume that if this really was a bug, someone else would have found it
by now.

</F>

Andrew Lee

unread,
Aug 28, 2008, 12:01:30 PM8/28/08
to

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

>>> for i in range(1,6):
... 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 *

0 new messages