小编Leo*_*oDT的帖子

在python中,将child附加到root.children(list),并将child.children(list)更改?

码:

class Node:
    def __init__(self, key, children=[]):
        self.key = key
        self.children = children

    def __repr__(self):
        return self.key
Run Code Online (Sandbox Code Playgroud)

执行:

root = Node("root")
child = Node("child")
root.children.append(child)
print child.children
print root.children[0].children
Run Code Online (Sandbox Code Playgroud)

结果:

[child]
[child]
Run Code Online (Sandbox Code Playgroud)

这真的很奇怪,为什么?

Python的版本是2.7.2.

python

1
推荐指数
1
解决办法
776
查看次数

标签 统计

python ×1