Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > List append

Reply
Thread Tools

List append

 
 
mouseit
Guest
Posts: n/a
 
      09-15-2007
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!

 
Reply With Quote
 
 
 
 
Stephen
Guest
Posts: n/a
 
      09-15-2007
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 <(E-Mail Removed)> 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!



 
Reply With Quote
 
 
 
 
mouseit
Guest
Posts: n/a
 
      09-15-2007
On Sep 14, 11:42 pm, Stephen <(E-Mail Removed)> 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 <(E-Mail Removed)> 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!

 
Reply With Quote
 
Rob E
Guest
Posts: n/a
 
      09-16-2007
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

 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      09-16-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: can we append a list with another list in Python ? David Hutto Python 0 10-25-2012 09:47 AM
Re: can we append a list with another list in Python ? Dave Angel Python 0 10-25-2012 09:26 AM
Re: can we append a list with another list in Python ? Daniel Fetchinson Python 0 10-23-2012 08:51 AM
Why doesn't python's list append() method return the list itself? dhruvbird Python 16 07-16-2010 05:40 PM
the address of list.append and list.append.__doc__ HYRY Python 10 09-26-2007 09:41 AM



Advertisments