List append

Discussion in 'Python' started by mouseit, Sep 15, 2007.

  1. mouseit

    mouseit Guest

    I'm trying to add an element to a list which is a property of an
    object, stored in an array. When I append to one element, all of the
    lists are appended!

    Example Code:

    class Test:
    array = []

    myTests = [Test() , Test() , Test()]
    print len(myTests[1].array)
    myTests[0].array.append( 5 )
    print len(myTests[1].array)

    prints:
    0
    1

    This is probably a really easy question (I'm very new to python), so
    thanks in advance for your help!
     
    mouseit, Sep 15, 2007
    #1
    1. Advertising

  2. mouseit

    Stephen Guest

    In your code, "array" is a class attribute, so it is shared among all
    instances. You need to use the __init__ method to define instance
    (data) attributes instead:

    def __init__(self):
    self.array = []

    On Sep 14, 11:25 pm, mouseit <> wrote:
    > I'm trying to add an element to a list which is a property of an
    > object, stored in an array. When I append to one element, all of the
    > lists are appended!
    >
    > Example Code:
    >
    > class Test:
    > array = []
    >
    > myTests = [Test() , Test() , Test()]
    > print len(myTests[1].array)
    > myTests[0].array.append( 5 )
    > print len(myTests[1].array)
    >
    > prints:
    > 0
    > 1
    >
    > This is probably a really easy question (I'm very new to python), so
    > thanks in advance for your help!
     
    Stephen, Sep 15, 2007
    #2
    1. Advertising

  3. mouseit

    mouseit Guest

    On Sep 14, 11:42 pm, Stephen <> wrote:
    > In your code, "array" is a class attribute, so it is shared among all
    > instances. You need to use the __init__ method to define instance
    > (data) attributes instead:
    >
    > def __init__(self):
    > self.array = []
    >
    > On Sep 14, 11:25 pm, mouseit <> wrote:
    >
    > > I'm trying to add an element to a list which is a property of an
    > > object, stored in an array. When I append to one element, all of the
    > > lists are appended!

    >
    > > Example Code:

    >
    > > class Test:
    > > array = []

    >
    > > myTests = [Test() , Test() , Test()]
    > > print len(myTests[1].array)
    > > myTests[0].array.append( 5 )
    > > print len(myTests[1].array)

    >
    > > prints:
    > > 0
    > > 1

    >
    > > This is probably a really easy question (I'm very new to python), so
    > > thanks in advance for your help!


    Awsome, that worked. Thanks!
     
    mouseit, Sep 15, 2007
    #3
  4. mouseit

    Rob E Guest

    On Sat, 15 Sep 2007 03:25:27 +0000, mouseit wrote:

    > I'm trying to add an element to a list which is a property of an
    > object, stored in an array. When I append to one element, all of the
    > lists are appended!
    >
    > Example Code:
    >
    > class Test:
    > array = []
    >
    > myTests = [Test() , Test() , Test()]
    > print len(myTests[1].array)
    > myTests[0].array.append( 5 )
    > print len(myTests[1].array)
    >
    > prints:
    > 0
    > 1
    >
    > This is probably a really easy question (I'm very new to python), so
    > thanks in advance for your help!


    Yes, that's easy:

    class myclass:
    var1 = []

    means that var1 is associated with the class. If you want an attribute:

    class myclass:
    def __init__ (self):
    self.var1 = []

    is the correct way.

    Rob
     
    Rob E, Sep 16, 2007
    #4
  5. mouseit

    Steve Holden Guest

    Rob E wrote:
    > On Sat, 15 Sep 2007 03:25:27 +0000, mouseit wrote:
    >
    >> I'm trying to add an element to a list which is a property of an
    >> object, stored in an array. When I append to one element, all of the
    >> lists are appended!
    >>
    >> Example Code:
    >>
    >> class Test:
    >> array = []
    >>
    >> myTests = [Test() , Test() , Test()]
    >> print len(myTests[1].array)
    >> myTests[0].array.append( 5 )
    >> print len(myTests[1].array)
    >>
    >> prints:
    >> 0
    >> 1
    >>
    >> This is probably a really easy question (I'm very new to python), so
    >> thanks in advance for your help!

    >
    > Yes, that's easy:
    >
    > class myclass:
    > var1 = []
    >
    > means that var1 is associated with the class. If you want an attribute:
    >
    > class myclass:
    > def __init__ (self):
    > self.var1 = []
    >
    > is the correct way.
    >

    It's easy to get confused, though, because when you try to access an
    instance attribute, if the attribute isn't found in the instance the
    interpreter will then look in the class, and then (if there is one) in
    the class's superclass, and so on.

    A further complication arises with methods, since even when the method
    is acquired by inheritance it is bound to the instance.

    >>> class Mine(object):

    .... pass
    ....
    >>> Mine.__init__

    <slot wrapper '__init__' of 'object' objects>
    >>> mine = Mine()
    >>> mine.__init__

    <method-wrapper '__init__' of Mine object at 0x7ff2c3cc>
    >>>


    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden

    Sorry, the dog ate my .sigline
     
    Steve Holden, Sep 16, 2007
    #5
    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. HYRY
    Replies:
    10
    Views:
    608
    Bruno Desthuilliers
    Sep 26, 2007
  2. dhruvbird
    Replies:
    16
    Views:
    1,343
    John Nagle
    Jul 16, 2010
  3. Daniel Fetchinson
    Replies:
    0
    Views:
    161
    Daniel Fetchinson
    Oct 23, 2012
  4. Dave Angel
    Replies:
    0
    Views:
    179
    Dave Angel
    Oct 25, 2012
  5. David Hutto
    Replies:
    0
    Views:
    175
    David Hutto
    Oct 25, 2012
Loading...

Share This Page