In python, if I have
x = y
any modification to x will also modify y, and I can do
x = deepcopy(y)
if I want to avoid modifying y while working on x
Say, instead, that I have:
myFunc():
return y
def main():
x = myFunc()
Is it still the case that modifying x will modify y, or since it is a return from another function it will be like a deepcopy?
解决方案
It will be a shallow copy, as nothing has been explicitly copied.
def foo(list):
list[1] = 5
return list
For example:
>>> listOne = [1, 2]
>>> listTwo = [3, 4]
>>> listTwo = listOne
>>> foo(listTwo)
[1, 5]
>>> listOne
[1, 5]