List append

M

mouseit

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!
 
S

Stephen

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 = []
 
M

mouseit

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 = []

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!
 
R

Rob E

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
 
S

Steve Holden

Rob said:
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.
.... pass
....
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top