这是我为我发现的问题创建的测试用例。
由于某种原因,B()中的dict()'l'似乎没有保持正确的值。请参阅我的Linux 11.04 Ubuntu上的输出,python 2.7.1 +。
class A():
name = None
b = None
def __init__(self, name, bname, cname, dname):
self.name = name
print "A: name", name
self.b = B(name, bname, cname, dname)
print "A self.b:", self.b
class B():
name = None
l = dict()
c = None
def __init__(self, name, bname, cname, dname):
self.aname = name
self.name = bname
print " B: name", bname
self.c = C(bname, cname, dname)
self.l["bb"] = self.c
print " B self:", self
print " B self.c:", self.c
print " B self.l[bb]:", self.l["bb"], "<<< OK >>>"
def dump(self):
print " A: name", self.aname
print " B: name", self.name
for i in self.l:
print " B: i=", i, "self.l[i]", self.l[i], "<<< ERROR >>>"
class C():
name = None
l = dict()
d = None
def __init__(self, bname, cname, dname):
self.bname = bname
self.cname = cname
print " B: name", bname
print " C: name", cname
print " C self:", self
def dump(self):
print " B name:", self.bname
print " C name:", self.cname
a1 = A("a1", "b1", "c1", "d1")
a2 = A("a2", "b2", "c2", "d2")
a3 = A("a3", "b3", "c3", "d3")
a1.b.dump()
a1.b.c.dump()
a2.b.dump()
a2.b.c.dump()
a3.b.dump()
a3.b.c.dump()
我机器上的输出:
$ python bedntest.py
A: name a1
B: name b1
B: name b1
C: name c1
C self: <__main__.C instance at 0xb76f3a6c>
B self: <__main__.B instance at 0xb76f388c>
B self.c: <__main__.C instance at 0xb76f3a6c>
B self.l[bb]: <__main__.C instance at 0xb76f3a6c> <<< OK >>>
A self.b: <__main__.B instance at 0xb76f388c>
A: name a2
B: name b2
B: name b2
C: name c2
C self: <__main__.C instance at 0xb76f3acc>
B self: <__main__.B instance at 0xb76f3aac>
B self.c: <__main__.C instance at 0xb76f3acc>
B self.l[bb]: <__main__.C instance at 0xb76f3acc> <<< OK >>>
A self.b: <__main__.B instance at 0xb76f3aac>
A: name a3
B: name b3
B: name b3
C: name c3
C self: <__main__.C instance at 0xb76f3b2c>
B self: <__main__.B instance at 0xb76f3b0c>
B self.c: <__main__.C instance at 0xb76f3b2c>
B self.l[bb]: <__main__.C instance at 0xb76f3b2c> <<< OK >>>
A self.b: <__main__.B instance at 0xb76f3b0c>
A: name a1
B: name b1
B: i= bb self.l[i] <__main__.C instance at 0xb76f3b2c> <<< ERROR >>>
B name: b1
C name: c1
A: name a2
B: name b2
B: i= bb self.l[i] <__main__.C instance at 0xb76f3b2c> <<< ERROR >>>
B name: b2
C name: c2
A: name a3
B: name b3
B: i= bb self.l[i] <__main__.C instance at 0xb76f3b2c> <<< ERROR >>>
B name: b3
C name: c3
根据我的理解,以上几行:
B: i= bb self.l[i] <__main__.C instance at 0xb76f3b2c> <<< ERROR >>>
应该都拥有C()的唯一实例,如初始化时所见 - 而不是最后创建的实例(请参阅&lt;&lt;&lt;&lt;&lt;&lt;&gt;&gt;&gt;行)。
这里发生了什么?
答案 0 :(得分:6)
发生了什么是您创建了一个类属性。通过在__init__()
中实例化来创建实例属性。
答案 1 :(得分:1)
看起来您正试图在类级别“声明”实例属性。类属性在Python中有自己的特定用途,如果你不想使用类属性将它们放在那里是错误的
class A():
name = None # Don't do this
b = None # Don't do this
def __init__(self, name, bname, cname, dname):
self.name = name
print "A: name", name
self.b = B(name, bname, cname, dname)
print "A self.b:", self.b
在class B
中,您创建了一个类属性l
。由于实例没有自己的属性l
,因此它使用了类的属性。
你可以像这样编写你的B类
class B():
def __init__(self, name, bname, cname, dname):
self.aname = name
self.name = bname
self.l = dict()
print " B: name", bname
self.c = C(bname, cname, dname)
self.l["bb"] = self.c
print " B self:", self
print " B self.c:", self.c
print " B self.l[bb]:", self.l["bb"], "<<< OK >>>"
...