声明了一个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