大規模なシステムのバグに完全に困惑しています。次のクラスを検討してください (各ノードは、その親へのポインターとその子のリストを保持します)。
class Node:
children = []
def __init__(self, parent):
self.contents = dict()
self.parent = parent
if parent is not None:
print self.children
print parent == self
parent.children.append(self)
print self.children
これを実行する:
foo1 = Node(None)
foo2 = Node(foo1)
不思議なことにこれを返します:
[]
False
[<__main__.Node instance at 0x10051f128>]
これはどのように意味がありますか?2 番目のノードの子が空でないのはなぜですか? おそらく、Python が参照を渡す方法に関連する概念の基本的な理解が欠けています。