聲明了一個node
類:
class Node:
feat = -1
child = []
label = -1
def __init__(self, feat, label):
self.feat = feat
self.label = label
分別創建兩個node
,並且將node2
放入node1
中的child
中:
node1 = Node(2, 0)
node2 = Node(3, 1)
print(node2.feat, node2.label)
node2.change(9)
node1.child.append(node2)
print(len(node1.child))
print(node1.child[0].feat, node1.child[0].label)
輸出結果:
3 1
1
9 1
在重新定義兩個初始值不一樣的node
,同樣的操作,卻發現node1
的child
個數變為了2
node1 = Node(2, 2)
node2 = Node(6, 1)
print(node2.feat, node2.label)
node2.change(15)
node1.child.append(node2)
print(len(node1.child))
print(node1.child[0].feat, node1.child[0].label)
遍歷node1.child
後發現,兩個node2
都在裡面:
for i in range(len(node1.child)):
print(node1.child[i].feat, node1.child[i].label)
輸出結果:
9 1
15 1