连接对象实例化

时间:2014-06-14 02:17:13

标签: python object instantiation

我已编写此代码来实例化网络(“noeud”在法语中意为“节点”):

class Noeud():
  def __init__(self, tableaux_ptrs_noeuds) :
    self.ptr_noeud_suivant = ptr_noeud_suivant


A = Noeud([D, H, K])
D = Noeud([A, G, H])
F = Noeud([K, L])
G = Noeud([D, H, J, M])
H = Noeud([A, D, G, K, M])
J = Noeud([G, L, M])
K = Noeud([A, F, H, L, M])
L = Noeud([F, J, K, M])
M = Noeud([G, H, J, K, L])

但我得到一个错误,因为A需要D H和K而且还没有定义。我怎么解决这个问题? 提前谢谢

1 个答案:

答案 0 :(得分:1)

BrenBarn正在描述这样的事情:

class Node:
    def __init__(self):
        self.connections = []

    def connectNodes(self, nodes):
        self.connections += nodes

A = Node()
D = Node()
F = Node()
G = Node()
H = Node()
J = Node()
K = Node()
L = Node()
M = Node()
N = Node()

A.connect([D, H, K])
D.connect([A, G, H])
F.connect([K, L])
# and so on.