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. output snippet: Question: 1) I do not expect the behavior seen in lines 8, 9 and 10. 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? I think I'm missing something fundamental, can someone help? < 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>
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
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;
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
Post a Comment