如果我使用列表作爲函數的參數,我希望原始列表不會被修改。爲什麼當我使用下面的代碼時,x == z
是真的呢應該是是x == range(10)
是真的嗎?爲什麼Python在這個函數中修改源代碼?
In [83]: def pop_three(collection):
....: new_collection = []
....: new_collection.append(collection.pop())
....: new_collection.append(collection.pop())
....: new_collection.append(collection.pop())
....: return new_collection, collection
....:
In [84]: x = range(10)
In [85]: x
Out[85]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [86]: y, z = pop_three(x)
In [87]: y
Out[87]: [9, 8, 7]
In [88]: z
Out[88]: [0, 1, 2, 3, 4, 5, 6]
In [89]: x
Out[89]: [0, 1, 2, 3, 4, 5, 6]
看到的頁面,http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference – han058 2014-09-25 00:23:42