我已經寫了一個小片段,計算給定節點的路徑長度遞歸函數的奇怪的行爲(例如其到根節點的距離):的Python:與關鍵字參數
def node_depth(node, depth=0, colored_nodes=set()):
"""
Return the length of the path in the parse tree from C{node}'s position
up to the root node. Effectively tests if C{node} is inside a circle
and, if so, returns -1.
"""
if node.mother is None:
return depth
mother = node.mother
if mother.id in colored_nodes:
return -1
colored_nodes.add(node.id)
return node_depth(mother, depth + 1, colored_nodes)
現在有一這個函數發生奇怪的事情(至少對我來說很奇怪):第一次調用node_depth返回正確的值。但是,使用同一個節點第二次調用它將返回-1。設置colored_nodes是在第一次調用空的,但包含了所有節點的ID在已經在第一個添加了第二個電話:
print node_depth(node) # --> 9
# initially colored nodes --> set([])
print node_depth(node) # --> -1
# initially colored nodes --> set([1, 2, 3, 38, 39, 21, 22, 23, 24])
print node_depth(node, colored_nodes=set()) # --> 9
print node_depth(node, colored_nodes=set()) # --> 9
我在這裏缺少一些具體的Python的事情,這是真的要是這樣嗎?
由於提前,
耶拿
同樣的事情咬我,我發現你的問題和另一個細節解釋。 http://stackoverflow.com/questions/1132941/least-astonishment-in-python-which-scope-is-the-mutable-default-argument-in – Yefei 2014-06-16 06:50:37