在python中,函数返回的是浅拷贝还是深拷贝? [英] In python, is a function return a shallow or deep copy?

查看:467
本文介绍了在python中,函数返回的是浅拷贝还是深拷贝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,如果我有

In python, if I have

x = y

对x的任何修改也会修改y,我可以做到

any modification to x will also modify y, and I can do

x = deepcopy(y)

如果我想避免在处理x时修改y

if I want to avoid modifying y while working on x

说,我有:

myFunc():
    return y

def main():
    x = myFunc()

修改x仍然会修改y,还是因为它是另一个函数的返回,所以它会像Deepcopy一样?

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

例如:

>>> listOne = [1, 2]
>>> listTwo = [3, 4]
>>> listTwo = listOne
>>> foo(listTwo)
[1, 5]
>>> listOne
[1, 5]

这篇关于在python中,函数返回的是浅拷贝还是深拷贝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆