By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

list of class instances within a list of a class instances

P: n/a
Hi,

I have a basic programming question regarding classes
in python. I want to have a list of "primaryClass"
instances, and in each instance of primaryClass I would
like a list of "subClass" instances. When each primaryClass
instance is created I want to put one instance of subClass
into the "sClasses" list (see code). The attached
code is what I came up with, but it doesn't do what I
want. For some reason the "sClasses" list in each primaryClass
instance is the same! That is, when a new primaryClass is instantiated,
and the .append(subClass()) is called in the constructor, the
subClass is appended to ALL instances of primaryClass, instead
of just the new instance of primaryClass. The
print statements in the code confirm this.

I think I must be missing something fundamental. Thanks
in advance.

jgw

================================================== ====================
################################################## #############################
class primaryClass:
name=""
sClasses = []

def __init__(self,name):
self.name = name
self.sClasses.append(subClass("default"))
################################################## #############################
class subClass:
name=""

def __init__(self,name):
self.name = name

################################################## #############################
port = [] # make a list
port.append(primaryClass("firstclass"))

print 'port contents'
for i in range(len(port)):
print i, port[i].name
for j in range(len(port[i].sClasses)):
print i, j, port[i].sClasses[j].name

port.append(primaryClass("secondclass"))
print 'port contents'
for i in range(len(port)):
print i, port[i].name
for j in range(len(port[i].sClasses)):
print i, j, port[i].sClasses[j].name

port.append(primaryClass("thirdclass"))
print 'port contents'
for i in range(len(port)):
print i, port[i].name
for j in range(len(port[i].sClasses)):
print i, j, port[i].sClasses[j].name
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
John Wohlbier wrote:
class primaryClass:
Remove the following two lines. Variables you put here are shared between
all instances of the class.
name=""
sClasses = []

def __init__(self,name): self.sClasses = [] # now you'll get a new list for each instance self.name = name
self.sClasses.append(subClass("default"))
################################################## ############################# class subClass:
The following line is superfluous.
name=""

def __init__(self,name):
self.name = name

################################################## #############################


port = [] # make a list
port.append(primaryClass("firstclass"))

print 'port contents'
More idiomatic is:
for p in port:
print p.name
for s in s.sClasses:
#...
Or, if you really need the indices:

for i, p in enumerate(port):
print i, p.name
#...
for i in range(len(port)):
print i, port[i].name
for j in range(len(port[i].sClasses)):
print i, j, port[i].sClasses[j].name

port.append(primaryClass("secondclass"))
Hey, I've seen the following section before. Never duplicate code - that's
what functions are for.
print 'port contents'
for i in range(len(port)):
print i, port[i].name
for j in range(len(port[i].sClasses)):
print i, j, port[i].sClasses[j].name

port.append(primaryClass("thirdclass"))
print 'port contents'
for i in range(len(port)):
print i, port[i].name
for j in range(len(port[i].sClasses)):
print i, j, port[i].sClasses[j].name

Peter

Jul 18 '05 #2

P: n/a
> class primaryClass:
name=""
sClasses = []

def __init__(self,name):
self.name = name
self.sClasses.append(subClass("default"))


Try the below instead.

class primaryClass:
def __init__(self,name):
self.name = name
self.sClasses = [subClass("default")]

In general, everything defined in the class-level scope is shared among
all instances of that class, even before creation. When you re-assign
'name' in __init__ in your original version, or really anywhere else,
'name' is no longer referring to the shared 'primaryClass.name' object,
it is referring to the object assigned. When you used
self.sClasses.append(), you were modifying the shared list in-place,
which is why all instances of primaryClass shared the list.

Also, you don't need to define all class variables (or really any)
outside in the class-level scope. It is convenient for some shared
immutables (strings, integers, tuples, floats, functions) on occasion,
but unless you know what you are doing with the mutables (lists,
dictionaries, arrays (from the array module), etc.), you should be careful.

- Josiah
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.