在python中定义a = b = class_name(value1)b = class_name(value2)之后,两个值相同且不同 [英] Two values are same and different after defining a=b=class_name(value1) b=class_name(value2) in python
问题描述
在下面的代码中,我理解 tree
(在代码中命名)和 parent
(在代码中命名)的打印应该不相同.但是我不明白,为什么 tree
总是根据 parent
的更新而更新?(此问题在此处继续上一个问题)
In the following code, I understand that the print of tree
(named in the code) and parent
(named in the code) should not be the same. But I do not understand, why the tree
is always updating according to the updating of parent
?
(This question continues the last question here)
代码:
class node(object):
def __init__(self, value):
self.value = value
self.children = []
def __repr__(self, level=0):
ret = "\t"*level+repr(self.value)+"\n"
for child in self.children:
ret += child.__repr__(level+1)
return ret
def add(self, nod):
self.children.append(nod)
list_0 = ['AA', 'BB', 'CC', 'DD']
tree = parent = node(list_0[0])
i=1
while i<len(list_0):
current_node = node(list_0[i])
parent.add(current_node)
parent = current_node
i+=1
print(tree)
print(parent)
输出:
'AA'
'BB'
'CC'
'DD'
'DD'
在代码中,很明显,每次循环中都会重新初始化 parent
,但是根据我的理解,当 parent
正在更新时, tree
不应相应地更新.因为一旦 parent
在循环中重新初始化,它便不再具有与 tree
相同的ID(可能是地址),然后是 tree
不会根据 parent
更改.
In the code, it is obvious that the parent
is re-initialized every time in the loop, but according to my understanding, while the parent
is updating, the tree
should not be updated accordingly. Because once the parent
is re-initialized in the loop, it then does not have the same id(maybe address) with the tree
anymore, then the tree
will not be changed according to the parent
.
问题:
-
为什么即使每次在循环中更改
id(parent)
,tree
仍会根据parent
进行更新?
tree
如何链接到循环内的 parent
?例如 node [1]
, node [2]
.(更新)
how the tree
is linking to the parent
which is inside the loop? e.g., node[1]
, node[2]
.(update)
感谢任何评论或解释,谢谢!
Appreciate any comment or explanation, thanks in advance!
推荐答案
我认为这是因为您的树始终指向根节点,而父节点却指向下一个节点.
I think its because your tree is always pointing to the root node, but parent is pointing to the next node.
让我们定义一些术语:
- 节点[0]:创建的第一个节点
- 节点[1]:创建了第二个节点
- ref(node [X]):引用节点X
- ref(node [0])->ref(node [1]):引用节点1作为子节点的节点0
第0步
tree = parent = node(list_0[0])
tree is pointing to ref(node[0])
parent is pointing to ref(node[0])
第1步(在while循环内)
Step 1 (inside the while loop)
parent = current_node
tree still pointing to ref(node[0]) -> ref(node[1])
parent is pointing to ref(node[1])
第n步
tree still pointing to ref(node[0]) -> ref(node[1]) -> .... -> ref(node[n])
parent is pointing to ref(node[n])
一种更图形化的解释方式:
A more graphic way to explain this:
# Step 0
tree, parent -> node[0]
# Step 1
tree -> node[0]
|
parent -> node[1]
# Step 2
tree -> node[0]
|
node[1]
|
parent -> node[2]
# Step n
tree -> node[0]
|
node[1]
|
...
|
parent -> node[n]
在此行中,一个节点被添加为另一个节点的子节点:
A node is being added as child of another node in this line:
parent.add(current_node)
因此,树始终指向树的根元素,而父对象始终指向树的最后一个元素.这就是ID不同的原因!
So tree is always pointing to the root element of the tree and parent is pointing to the last element of the tree. That's the reason why the ids are different!
希望我能为您提供有用的信息!
I hope I have provided you with useful information!
这篇关于在python中定义a = b = class_name(value1)b = class_name(value2)之后,两个值相同且不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!