深浅拷贝
Python源码对深浅拷贝的解释
| # 英文原文 |
| The difference between shallow and deep copying is only relevant for |
| compound objects (objects that contain other objects, like lists or |
| class instances). |
| |
| - A shallow copy constructs a new compound object and then (to the |
| extent possible) inserts *the same objects* into it that the |
| original contains. |
| |
| - A deep copy constructs a new compound object and then, recursively, |
| inserts *copies* into it of the objects found in the original. |
| |
| Two problems often exist with deep copy operations that don't exist |
| with shallow copy operations: |
| |
| a) recursive objects (compound objects that, directly or indirectly, |
| contain a reference to themselves) may cause a recursive loop |
| |
| b) because deep copy copies *everything* it may copy too much, e.g. |
| administrative data structures that should be shared even between |
| copies |
| |
| Python's deep copy operation avoids these problems by: |
| |
| a) keeping a table of objects already copied during the current |
| copying pass |
| |
| b) letting user-defined classes override the copying operation or the |
| set of components copied |
| |
| This version does not copy types like module, class, function, method, |
| nor stack trace, stack frame, nor file, socket, window, nor any |
| similar types. |
| # 中文翻译 |
| 浅拷贝和深拷贝的区别仅在于复合对象(包含其他对象的对象,如列表或类实例)方面。 |
| |
| - 浅拷贝构建一个新的复合对象,然后(在可能的范围内)将*与原对象相同的对象*插入其中。 |
| - 深拷贝构建一个新的复合对象,然后递归地插入其中原始对象的*副本*。 |
| |
| 深拷贝操作通常存在两个问题,而浅拷贝操作则不存在: |
| |
| a) 递归对象(直接或间接地包含对自身引用的复合对象)可能导致递归循环 |
| |
| b) 因为深拷贝会复制*所有内容*,可能复制过多,例如,应该在拷贝之间共享的管理数据结构 |
| |
| Python 的深拷贝操作通过以下方式避免这些问题: |
| |
| a) 在当前拷贝过程中保持一个已复制对象的表 |
| |
| b) 允许用户定义的类覆盖拷贝操作或要复制的组件集 |
| |
| 这个版本不会复制诸如模块、类、函数、方法、堆栈跟踪、堆栈帧、文件、套接字、窗口等类型。 |
代码解释
| import copy |
| |
| copy_old = [1, 2, [3, 4]] |
| copy_new = copy.copy(copy_old) |
| copy_old.append(5) |
| print(copy_new, id(copy_new)) |
| print(copy_old, id(copy_old)) |
| |
| |
| copy_old = [1, 2, [3, 4]] |
| copy_new = copy.copy(copy_old) |
| copy_old[2].append(5) |
| print(copy_new, id(copy_new)) |
| print(copy_old, id(copy_old)) |
| |
| print('浅拷贝与深拷贝的区别'.center(20,'>')) |
| |
| |
| copy_old = [1, 2, [3, 4]] |
| copy_new = copy.deepcopy(copy_old) |
| copy_old.append(5) |
| copy_old[2].append(5) |
| print(copy_new, id(copy_new)) |
| print(copy_old, id(copy_old)) |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了