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

Discussion in 'Python' started by Bart van Deenen, Aug 22, 2008.

  1. 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
    Bart van Deenen, Aug 22, 2008
    #1
    1. Advertising

  2. Bart van Deenen

    Guest

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

    On Aug 22, 11:13 am, Bart van Deenen
    <> 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
    , Aug 22, 2008
    #2
    1. Advertising

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

    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.

    wrote:

    > On Aug 22, 11:13 am, Bart van Deenen
    > <> 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."
    >
    > 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
    Bart van Deenen, Aug 22, 2008
    #3
  4. 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
    Diez B. Roggisch, Aug 22, 2008
    #4
  5. Re: def X(l=[]): weirdness. Python bug ?

    Bart van Deenen a écrit :
    (ot : please don't top post - corrected)
    > wrote:
    >
    >> On Aug 22, 11:13 am, Bart van Deenen
    >> <> 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',)
    Bruno Desthuilliers, Aug 22, 2008
    #5
  6. 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
    Bart van Deenen, Aug 22, 2008
    #6
  7. Bart van Deenen

    Terry Reedy Guest

    How to

    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
    Terry Reedy, Aug 22, 2008
    #7
  8. 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>
    Fredrik Lundh, Aug 23, 2008
    #8
  9. Bart van Deenen

    Andrew Lee Guest

    Bart van Deenen 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
    >


    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 *
    Andrew Lee, Aug 28, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiong Feng
    Replies:
    0
    Views:
    812
    Jiong Feng
    Nov 19, 2003
  2. Sean Ross
    Replies:
    3
    Views:
    119
    Aredridel
    Dec 25, 2003
  3. Replies:
    7
    Views:
    154
  4. planetthoughtful

    Newbie: def must come before call to def?

    planetthoughtful, Mar 12, 2007, in forum: Ruby
    Replies:
    4
    Views:
    125
    Pit Capitain
    Mar 12, 2007
  5. Kyung won Cheon
    Replies:
    0
    Views:
    200
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page