defaults for function arguments bound only once(??)

Discussion in 'Python' started by horos11, Oct 4, 2009.

  1. horos11

    horos11 Guest

    All,

    Another one, this time a bit shorter.

    It looks like defaults for arguments are only bound once, and every
    subsequent call reuses the first reference created. Hence the
    following will print '[10,2]' instead of the expected '[1,2]'.

    Now my question - exactly why is 'default_me()' only called once, on
    the construction of the first object? And what's the best way to get
    around this if you want to have a default for an argument which so
    happens to be a reference or a new object?

    ---- code begins here ---

    import copy
    class A:

    def default_me():
    return [1,2]

    def __init__(self, _arg=default_me()):
    self.arg = _a


    a = A()
    a.arg[0] = 10
    b = A()

    print b.arg # prints [10,2]
    horos11, Oct 4, 2009
    #1
    1. Advertising

  2. horos11

    Chris Rebert Guest

    On Sat, Oct 3, 2009 at 11:29 PM, horos11 <> wrote:
    > All,
    >
    > Another one, this time a bit shorter.
    >
    > It looks like defaults for arguments are only bound once, and every
    > subsequent call reuses the first reference created. Hence the
    > following will print '[10,2]' instead of the expected '[1,2]'.
    >
    > Now my question - exactly why is 'default_me()' only called once, on
    > the construction of the first object?


    Actually, the single call happens at the "definition-time" of the
    function. As for why, it's less magical than re-evaulating it on every
    function call when that parameter is not supplied a value; trust me,
    the issue has been argued to death.

    > And what's the best way to get
    > around this if you want to have a default for an argument which so
    > happens to be a reference or a new object?


    See http://docs.python.org/tutorial/controlflow.html#default-argument-values
    Essentially, use None as the default value, then check for it and
    assign the real default value in the function body.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Oct 4, 2009
    #2
    1. Advertising

  3. horos11

    Simon Forman Guest

    On Sun, Oct 4, 2009 at 2:29 AM, horos11 <> wrote:
    > All,
    >
    > Another one, this time a bit shorter.
    >
    > It looks like defaults for arguments are only bound once, and every
    > subsequent call reuses the first reference created. Hence the
    > following will print '[10,2]' instead of the expected '[1,2]'.
    >
    > Now my question - exactly why is 'default_me()' only called once, on
    > the construction of the first object? And what's the best way to get
    > around this if you want to have a default for an argument which so
    > happens to be a reference or a new object?


    This is a FAQ: http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects

    >
    > ---- code begins here ---
    >
    > import copy
    > class A:
    >
    >    def default_me():
    >        return [1,2]
    >
    >    def __init__(self, _arg=default_me()):
    >        self.arg = _a
    >
    >
    > a = A()
    > a.arg[0] = 10
    > b = A()
    >
    > print b.arg  # prints [10,2]


    Your code is weird: you import copy module but don't use it; you
    define default_me() as a "sort of" static method
    (http://docs.python.org/library/functions.html#staticmethod) but then
    only use it to generate a default argument that has nothing to do with
    the class you just defined; and in __init__() you use "_a" which isn't
    defined anywhere in this code snippet.

    What are you actually trying to accomplish?
    Simon Forman, Oct 4, 2009
    #3
    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. sunil panda

    Lower bound & Upper bound

    sunil panda, Dec 25, 2003, in forum: Java
    Replies:
    9
    Views:
    7,206
    thushara wijeratna
    Oct 7, 2008
  2. Rhiner Dan
    Replies:
    1
    Views:
    737
    Mike Wahler
    Mar 27, 2005
  3. Mario Krsnic
    Replies:
    0
    Views:
    366
    Mario Krsnic
    Jun 23, 2006
  4. jmborr
    Replies:
    1
    Views:
    398
    Stargaming
    Nov 3, 2007
  5. Replies:
    4
    Views:
    97
    Gene Tani
    Mar 11, 2006
Loading...

Share This Page