On Aug 22, 11:13 am, Bart van Deenen
<[EMAIL PROTECTED]> wrote:
> 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

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to