473,230 Members | 1,855 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 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 2784
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
5
by: oliver | last post by:
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a...
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
2
by: PRadyut | last post by:
In this code i tried to add the elements in ascending order but the output is only 0 1 2 the rest of the elements are not shown. the code...
3
by: Kriston-Vizi Janos | last post by:
Dear Mr. Kern, and Members, Thank you very much for the fast answer, my question became over-simplified. My source code is appended below. It uses two text files (L.txt and GC.txt) as input...
26
by: Swroteb | last post by:
Hi there, I've got a reasonably sized list of objects that I'd like to pull out all combinations of five elements from. Right now I have a way to do this that's quite slow, but manageable. I...
7
by: Lonnie Princehouse | last post by:
Here's a curious hack I want to put up for discussion. I'm thinking of writing a PEP for it. Observation ----------------- I found myself using this construct for assembling multiple lists: ...
2
by: jeremito | last post by:
I have created a class that inherits from the list object. I want to override the append function to allow my class to append several copies at the same time with one function call. I want to do...
10
by: HYRY | last post by:
I have the following questions, I am using Python 2.4.2 19167152 #1 11306608 #1 1. the address of a.append and list.append is different, can I get the address of...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.