unexpected class behaviour

Discussion in 'Python' started by Jan Schäfer, Sep 7, 2008.

  1. Hi all,

    can anyone explain the behaviour of the following code sniplet:

    ---> schnipp <---
    class Base(object):
    def __init__( self, lst=[] ):
    self.varlist = lst

    def addVar( self, var ):
    self.varlist.append(var)

    class Derived(Base):
    def __init__( self, var ):
    Base.__init__(self)
    self.addVar(var)

    vars = ['foo', 'bar']

    for ivar in vars:
    obj = Derived(ivar)
    print ivar, obj, obj.varlist
    ---> schnapp <---

    After running (Python 2.5.1), I get the following output:
    foo <__main__.Derived object at 0xb7c608cc> ['foo']
    bar <__main__.Derived object at 0xb7c6092c> ['foo', 'bar']

    So, I get two different objects, but how does the 'foo' get into the second
    varlist? I'm a little bit confused about this, any ideas?

    Thanks in advance

    Jan
    Jan Schäfer, Sep 7, 2008
    #1
    1. Advertising

  2. Jan Schäfer wrote:

    > can anyone explain the behaviour of the following code sniplet:


    well, it *is* explained in the tutorial, the language reference, and the
    FAQ, so yes, it can be explained ;-)

    for more information, see this page:

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

    </F>
    Fredrik Lundh, Sep 7, 2008
    #2
    1. Advertising

  3. Jan Schäfer

    kaer Guest

    On 7 sep, 12:40, Fredrik Lundh <> wrote:
    > Jan Schäfer wrote:
    > > can anyone explain the behaviour of the following code sniplet:

    >
    > well, it *is* explained in the tutorial, the language reference, and the
    > FAQ, so yes, it can be explained ;-)
    >
    > for more information, see this page:
    >
    > http://effbot.org/zone/default-values.htm
    >
    > </F>


    Well, you may want replace the last line by:
    print ivar, obj, obj.varlist, id(obj.varlist)

    To have another behavior, you may want replace the 3 first lines by:
    class Base(object):
    def __init__( self, lst=None ):
    if lst is None: lst=[]
    self.varlist = lst

    Enjoy Python !
    kaer, Sep 7, 2008
    #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. Mark
    Replies:
    4
    Views:
    2,939
    scoude
    Jan 12, 2011
  2. Steven Van den Berghe

    unexpected map behaviour

    Steven Van den Berghe, Aug 28, 2003, in forum: C++
    Replies:
    2
    Views:
    405
    Christian Janßen
    Aug 28, 2003
  3. Old Wolf
    Replies:
    1
    Views:
    379
    Victor Bazarov
    Feb 4, 2004
  4. Ioannis Vranos

    Unexpected behaviour

    Ioannis Vranos, Sep 23, 2004, in forum: C++
    Replies:
    36
    Views:
    896
    Rolf Magnus
    Sep 24, 2004
  5. Richard Philips

    Unexpected python behaviour

    Richard Philips, Nov 28, 2003, in forum: Python
    Replies:
    2
    Views:
    305
    Jay O'Connor
    Nov 28, 2003
Loading...

Share This Page