这是代码:
class Foo:
def __init__(self):
self.l = [1, 2, 3]
def get_list(self):
return self.l
f = Foo()
l2 = f.get_list()
print(f.get_list())
l2.remove(2)
print(l2)
print(f.get_list())
这是运行:
>python /tmp/1.py
[1, 2, 3]
[1, 3]
[1, 3]
所以函数 get_list() 返回引用。
有没有办法让它返回值?