我有一个root容器类。此容器具有将保存对象的列表成员。这些对象中的每一个还包含子对象列表,并且这些子对象又保持其他子对象。我想使用列表,因为订购对于手头的应用程序很重要。
问题:我看到子类的对象列表不包含正确的对象集,并且列表的长度不是我所期望的。
这是一些示例代码(代码可以复制和执行),非常简单。我使用了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()
输出代码段:
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
问题:
1)在第8,9和10行看到的行为不符合我的预期。
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.
如果刚刚添加了一个对象后新创建的对象的长度为1,那么第10行的长度是3。
2)当打印列表的内容时,我看到更多的项目被添加到我期望的列表中。我原本期望S2对象中只有2个C对象?为什么会这样?
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>
我认为我缺少一些基本的东西,有人可以帮忙吗?
答案 0 :(得分:1)
class mycls2:
olist = []
class mycls1:
olist = []
class mycl:
olist = []
在所有这些类中,olist
是一个类变量。因此,这些类的所有实例将共享与其类相对应的olist
。
您需要的是实例变量。
class mycls2:
def __init__(self):
self.olist = []
class mycls1:
def __init__(self):
self.olist = []
class mycl:
def __init__(self):
self.olist = []