471,344 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

List append

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!

Sep 15 '07 #1
4 2715
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 <mouseit...@gmail.comwrote:
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!

Sep 15 '07 #2
On Sep 14, 11:42 pm, Stephen <Obey....@gmail.comwrote:
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 <mouseit...@gmail.comwrote:
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!

Sep 15 '07 #3
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

Sep 16 '07 #4
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

Sep 16 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Jess Austin | last post: by
5 posts views Thread by oliver | last post: by
5 posts views Thread by Dream Catcher | last post: by
25 posts views Thread by Yves Glodt | last post: by
2 posts views Thread by PRadyut | last post: by
3 posts views Thread by Kriston-Vizi Janos | last post: by
26 posts views Thread by Swroteb | last post: by
7 posts views Thread by Lonnie Princehouse | last post: by
2 posts views Thread by jeremito | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.