Connecting Tech Pros Worldwide Help | Site Map

List append

mouseit
Guest
 
Posts: n/a
#1: Sep 15 '07
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
Guest
 
Posts: n/a
#2: Sep 15 '07

re: List append


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:
Quote:
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
Guest
 
Posts: n/a
#3: Sep 15 '07

re: List append


On Sep 14, 11:42 pm, Stephen <Obey....@gmail.comwrote:
Quote:
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:
>
Quote:
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!
>
Quote:
Example Code:
>
Quote:
class Test:
array = []
>
Quote:
myTests = [Test() , Test() , Test()]
print len(myTests[1].array)
myTests[0].array.append( 5 )
print len(myTests[1].array)
>
Quote:
prints:
0
1
>
Quote:
This is probably a really easy question (I'm very new to python), so
thanks in advance for your help!
Awsome, that worked. Thanks!

Rob E
Guest
 
Posts: n/a
#4: Sep 16 '07

re: List append


On Sat, 15 Sep 2007 03:25:27 +0000, mouseit wrote:
Quote:
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

Steve Holden
Guest
 
Posts: n/a
#5: Sep 16 '07

re: List append


Rob E wrote:
Quote:
On Sat, 15 Sep 2007 03:25:27 +0000, mouseit wrote:
>
Quote:
>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.
Quote:
Quote:
Quote:
>>class Mine(object):
.... pass
....
Quote:
Quote:
Quote:
>>Mine.__init__
<slot wrapper '__init__' of 'object' objects>
Quote:
Quote:
Quote:
>>mine = Mine()
>>mine.__init__
<method-wrapper '__init__' of Mine object at 0x7ff2c3cc>
Quote:
Quote:
Quote:
>>>
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

Closed Thread