Python Fundamental Issue Seen When Creating List of Objects That Hold Other Objects -


I have a class is basically a container which contains a list of members in the container that holds the object. Each of these items has a list of sub-commodities and these objects are in turn replaced by other sub-objects. I want to use lists because the order of application in hand is to be ordered.

Problem: I see that the list of objects in subclasses does not include the exact set of objects and the length of the lists I do not expect.

Here is some example code (code can be copied and executed) and it is quite straight forward. I use Python 2.6.2. class mycls2: olist = [] class mycls1: olist = [] class mycl: olist = [] class myc: x = 0 def __init __ (auto, i): range (0, 2) in: self.x = i def test_obj1 (): kctr = 0 mylist = mycl (): s1 = mycls1 () print for "i = 1" s1: ", s1 for category (0, 2) : S2 = mycls2 () print "s2:", for category (2, 2) s2: c = myc (kctr) kctr + = 1 print "c:", c s2.olist.append (c) print " Adding C "C," S 2 "S2," lane ", the lane (s 2klist) s1.olist.append (s2) Print" S add 2 ", S 2," S1 " S1, "lane", lane (s1.olist) mylist.olist.append (s1) add print "S1", s1, "mL" Merilist, "lane", lane (mylist.olist) Rint '===== ======================================== Print " MyCL1 list: "lane (mylist.olist) for mycl1_iter in mylist.olist: print mycl1_iter in mycl1_iter.olist" myCL2List: "for mycl2_iter, lane (mycl1_iter.olist): print mycl2_iter Print" MyCL: ", mycl2_iter lane (mycl2_iter.olist) for mycl_ter in .olist: Print mycl_ter print ================================= == ============= if '__ Nam__ == "__man__": test_obj1 ()

output snippet:

 < Code> line: 1 s1: & lt; __ Main __. Mycls1 Example 0x023 ABC 60 & gt; Line: 2 s2: & lt; __ Main __. Mycls2 example 0x023 ABC 88 & gt; Line: 3C: & lt; __ Main __. Myc Example 0x023 ABCB 0 on & gt; Line: Add 4 C & lt; __ Main __. Myc Example 0x023ABCB0 & gt; For S2 & lt; __ Main __. Mycls2 example 0x023 ABC 88 & gt; Lane 1 Line: 5C: & lt; __ Main __. Myc Example 0x023 ABCD 8 & gt; Line: Add 6 C & lt; __ Main __. Myc Example 0x023 ABCD 8 & gt; For S2 & lt; __ Main __. Mycls2 example 0x023 ABC 88 & gt; Lane: 2 Line: Adding 7 S2 & lt; __ Main __. Mycls2 example 0x023 ABC 88 & gt; For S1 & lt; __ Main __. Mycls1 Example 0x023 ABC 60 & gt; Lane: 1 Line: 8S2: & lt; __ Main __. Mycls2 example 0x023ABD00 & gt; Line: 9C: & lt; __ Main __. Myc Example at 0x023ABD28 & gt; Line: Adding 10 C & lt; __ Main __. Myc Example 0x023ABD28 & gt; For S2 & lt; __ Main __. Mycls2 example 0x023ABD00 & gt; Lane 3: << / code> 

Question:

1) I do not expect the behavior seen in lines 8, 9 and 10.

  line: 8s2: & lt; __ Main __. Mycls2 example 0x023ABD00 & gt; & Lt; - New S2 object built here Line: 9C: & lt; __ Main __. Myc Example 0x023ABD28 & gt; & Lt; - New C object line created here: add 10 C & lt; __ Main __. Myc Example 0x023ABD28 & gt; For S2 & lt; __ Main __. Mycls2 example 0x023ABD00 & gt; Lane: 3 & lt; - Adding the new S2 to the newly created C  

How can it be that the length is 3 in 10 when the length of the length should be 1 after the new object is added to an object.

2) The contents of the list I see that more items have been added to those lists which I expected would I just be expecting 2C objects here in the S2 object? Why is this happening?

  line: 43 & lt; __ Main __. Mycls2 example 0x023ABD00 & gt; & Lt; - Here only 2C objects are expected Line: 44 Micro: 8 Line: 45 & lt; __ Main __. Myc Example 0x023 ABCB 0 on & gt; Line: 46 & lt; __ Main __. Myc Example 0x023 ABCD 8 & gt; Line: 47 & lt; __ Main __. Myc Example 0x023ABD28 & gt; Line: 48 & lt; __ Main __. Myc Example 0x023ABD50 & gt; Line: 49 & lt; __ Main __. Myc Example 0x023ABDC8 & gt; Line: 50 & lt; __ Main __. Myc Example 0x023ABDF0 & gt; Line: 51 & lt; __ Main __. Myc Example 0x023ABE40 & gt; Line: 52 & lt; __ Main __. Myc Example at 0x023ABE68 & gt; Line: 53 & lt; __ Main __. Mycls2 Example 0x023ABDA0 at & gt;  

I think I'm missing something fundamental, can someone help?

mycls2: olist = [] class mycls1: olist = [] class mycl: olist = [] < / Code>

In all these classes, olist is a class variable, therefore, all examples of these classes will share olist for their class.

You need the frequency variable.

  class mycls2: def __init __ (self): self.olist = [] class mycls1: def __init __ (self): self.olist = [] class mycl: def __init __ (self ): Self.olist = []  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -