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

Discussion in 'Python' started by Mike, Apr 15, 2007.

  1. Mike

    Mike Guest

    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.

    >>> def f(l, r = []):

    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
     
    Mike, Apr 15, 2007
    #1
    1. Advertising

  2. Mike

    Troy Melhase Guest

    Re: function with list argument defaulting to [] - what's going onhere???

    On 4/14/07, Mike <> wrote:
    > 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.
     
    Troy Melhase, Apr 15, 2007
    #2
    1. Advertising

  3. Mike

    Mike Guest

    Re: function with list argument defaulting to [] - what's going onhere???

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

    Mike

    Troy Melhase wrote:
    > On 4/14/07, Mike <> wrote:
    >> 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.
     
    Mike, Apr 15, 2007
    #3
  4. Re: function with list argument defaulting to [] - what's going on here???

    On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:

    > On 4/14/07, Mike <> wrote:
    >> 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.




    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?



    --
    Steven.
     
    Steven D'Aprano, Apr 15, 2007
    #4
  5. Alex Martelli, Apr 15, 2007
    #5
  6. Mike

    Paddy Guest

    On Apr 15, 3:58 am, Steven D'Aprano
    <> wrote:
    > On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:
    > > On 4/14/07, Mike <> wrote:
    > >> 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.

    >
    > > fromhttp://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?
    >
    > --
    > Steven.


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

    - Paddy.
     
    Paddy, Apr 15, 2007
    #6
  7. Re: function with list argument defaulting to [] - what's going onhere???

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

    --
    mvh Björn
     
    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=, Apr 15, 2007
    #7
  8. Re: function with list argument defaulting to [] - what's going on here???

    On Sun, 15 Apr 2007 05:29:01 +0200, BJörn Lindqvist wrote:

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



    --
    Steven.
     
    Steven D'Aprano, Apr 15, 2007
    #8
  9. Re: function with list argument defaulting to [] - what's going on here???

    Steven D'Aprano <> wrote:

    > On Sun, 15 Apr 2007 05:29:01 +0200, BJörn Lindqvist wrote:
    >
    > >> 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
     
    Alex Martelli, Apr 15, 2007
    #9
  10. Mike

    Tim Leslie Guest

    Re: function with list argument defaulting to [] - what's going onhere???

    On 14 Apr 2007 20:20:42 -0700, Paddy <> wrote:
    > On Apr 15, 3:58 am, Steven D'Aprano
    > <> wrote:
    > > On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:
    > > > On 4/14/07, Mike <> wrote:
    > > >> 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.

    > >
    > > > fromhttp://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?
    > >
    > > --
    > > Steven.

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


    It is a check done by pylint

    Tim

    >
    > - Paddy.
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Tim Leslie, Apr 15, 2007
    #10
    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. Replies:
    2
    Views:
    618
  2. Jeremy Chapman

    Custom control, defaulting style properties

    Jeremy Chapman, Mar 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    385
    Jeremy Chapman
    Mar 7, 2006
  3. Jyrki Keisala

    Defaulting empty XML elements

    Jyrki Keisala, Jan 1, 2004, in forum: XML
    Replies:
    4
    Views:
    520
    Kenneth Stephen
    Jan 2, 2004
  4. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    428
  5. Ben Kial
    Replies:
    1
    Views:
    720
    Eric Enright
    Nov 15, 2004
Loading...

Share This Page