2
class Tree:
def __init__(self, label, children = []):
self.label = label
self.children = children
t1 = Tree("START")
t2 = Tree("END")
t1.children.append(t2)
print t2.children
[<__main__.Tree instance at 0x005421C0>]
爲什麼不是t2.children爲空?在構造函數中實例化列表的問題
看看上面的鏈接:每個人都被這個問題最終會感到驚訝。對於Python的方式建議,以解決它,看看http://stackoverflow.com/questions/366422/what-is-the-pythonic-way-to-avoid-default-parameters-that-are-empty-lists –