Question

I have a class that is a root container. This container has a list member that will hold objects. Each of these objects further holds a list of sub objects and these sub objects in turn hold other sub objects. I would like to use lists because ordering matters for the application at hand.

Issue: I am seeing that the lists of objects of the sub class do not contain the right set of objects and the length of the lists is not what I expected.

Here is some example code (code can be copied and executed) and is pretty straight forward. I used python 2.6.2.

class mycls2:
  olist = []

class mycls1:
  olist = []

class mycl:
  olist = []

class myc:
  x = 0
  def __init__(self, i):
    self.x = i

def test_obj1():
  kctr = 0
  mylist = mycl()
  for i in range(0, 2):
    s1 = mycls1()
    print "s1: ", s1
    for j in range(0, 2):
      s2 = mycls2()
      print "s2: ", s2
      for k in range(0, 2):
        c = myc(kctr)
        kctr += 1
        print " c: ", c
        s2.olist.append(c)
        print "Adding C ", c, " to S2 ", s2, " Len:", len(s2.olist)
      s1.olist.append(s2)
      print "Adding S2 ", s2, " to S1 ", s1, " Len:", len(s1.olist)
    mylist.olist.append(s1)
    print "Adding S1 ", s1, " to ML ", mylist, " Len:", len(mylist.olist)

  print '========================================'
  print "MyCL1List:", len(mylist.olist)
  for mycl1_iter in mylist.olist:
    print mycl1_iter
    print "MyCL2List:", len(mycl1_iter.olist)
    for mycl2_iter in mycl1_iter.olist:
      print mycl2_iter
      print "MyCL:", len(mycl2_iter.olist)
      for mycl_ter in mycl2_iter.olist:
        print mycl_ter
  print '========================================'

if __name__ == "__main__":
  test_obj1()

Output Snippet:

Line: 1 s1: <__main__.mycls1 instance at 0x023ABC60>
Line: 2 s2: <__main__.mycls2 instance at 0x023ABC88>
Line: 3 c: <__main__.myc instance at 0x023ABCB0>
Line: 4 Adding C <__main__.myc instance at 0x023ABCB0> to S2 <__main__.mycls2 instance at 0x023ABC88> Len: 1
Line: 5 c: <__main__.myc instance at 0x023ABCD8>
Line: 6 Adding C <__main__.myc instance at 0x023ABCD8> to S2 <__main__.mycls2 instance at 0x023ABC88> Len: 2
Line: 7 Adding S2 <__main__.mycls2 instance at 0x023ABC88> to S1 <__main__.mycls1 instance at 0x023ABC60> Len: 1
Line: 8 s2: <__main__.mycls2 instance at 0x023ABD00>
Line: 9 c: <__main__.myc instance at 0x023ABD28>
Line: 10 Adding C <__main__.myc instance at 0x023ABD28> to S2 <__main__.mycls2 instance at 0x023ABD00> Len: 3

Questions:

1) The behavior seen at lines 8, 9 and 10 is not what I expected.

Line: 8 s2: <__main__.mycls2 instance at 0x023ABD00> <-- New S2 object created here

Line: 9 c: <__main__.myc instance at 0x023ABD28> <-- New c object created here

Line: 10 Adding C <__main__.myc instance at 0x023ABD28> to S2 <__main__.mycls2 instance at 0x023ABD00> Len: 3 <-- Adding newly created c to new s2.

How is it that the length is 3 at Line 10 when the newly created object should have length 1 after having just added an object.

2) When printing the contents of the list I see that more items are added to the list that I expected. I would have expected only 2 C objects here in the S2 object? Why is this occurring?

Line: 43 <__main__.mycls2 instance at 0x023ABD00>  <-- Would have expected only 2 C objects here
Line: 44 MyCL: 8
Line: 45 <__main__.myc instance at 0x023ABCB0>
Line: 46 <__main__.myc instance at 0x023ABCD8>
Line: 47 <__main__.myc instance at 0x023ABD28>
Line: 48 <__main__.myc instance at 0x023ABD50>
Line: 49 <__main__.myc instance at 0x023ABDC8>
Line: 50 <__main__.myc instance at 0x023ABDF0>
Line: 51 <__main__.myc instance at 0x023ABE40>
Line: 52 <__main__.myc instance at 0x023ABE68>
Line: 53 <__main__.mycls2 instance at 0x023ABDA0>

I think I am missing something fundamental, can some one please help?

Was it helpful?

Solution

class mycls2:
  olist = []

class mycls1:
  olist = []

class mycl:
  olist = []

In all these classes, olist is a class variable. So, all the instances of these classes will share the olist corresponding to their class.

What you need is instance variables.

class mycls2:
  def __init__(self):
    self.olist = []

class mycls1:
  def __init__(self):
    self.olist = []

class mycl:
  def __init__(self):
    self.olist = []
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top