【发布时间】:2011-01-20 07:02:35
【问题描述】:
如果我要运行这段代码:
def function(y):
y.append('yes')
return y
example = list()
function(example)
print(example)
为什么即使我没有直接更改变量“example”,它也会返回 ['yes'],以及如何修改代码以使“example”不受函数影响?
【问题讨论】:
-
effbot.org/zone/default-values.htm 描述了一个不同但有些相关的问题。阅读这可能有助于您了解正在发生的事情。
-
另一种看待它的方式:您可以附加到函数中的列表,这非常有用。
标签: python list function arguments