笔记-python-standard library-8.10 copy
笔记-python-standard library-8.10 copy
1. copy
source code:Lib/copy.py
python中的赋值语句不复制对象,它创建了对象和目标之间的指向/绑定。
对于可变对象来说,有时需要一个复制体,而非引用,本模块提供了shallow copy和deep copy操作。
接口函数:
copy.copy(x) 返回一个shallow copy
copy.deepcopy(x) 返回一个deep copy
exception copy.error 抛出模块特定错误
浅复制和深复制的区别:
- 两者之间的区别主要是针对复杂对象而言的,例如列表和类实例。
- 浅复制复制对象但不复制引用体。
- 深复制复制对象,同时递归的去寻找它在原始对象的内容并复制。
深复制通常会存在两个问题,而浅复制不会:
- 递归对象可能会导致递归循环;
- 深复制可能会复制太多不必要的内容,比如应该在两个对象之间共享的内容。
针对上述问题,深拷贝通过以下方式避免:
- 保留一个“memo”字典用于描述拷贝的对象。
- 让用户定义的类覆盖掉复制操作或复制过来的组件。
1.1.1. example
import copy
la = [1, 2, 3, ['a', 'b', 'c']]
lb = la
print(id(lb) == id(la)) #True
# copy
lb = copy.copy(la)
print(id(lb) == id(la)) #False
print(id(lb[3]) == id(la[3])) #True
# deep copy
lb = copy.deepcopy(la)
print(id(lb) == id(la)) #False
print(id(lb[3]) == id(la[3])) #False
解释:
python中变量全是对象/引用,因此变量都是指向的内存区域的一个引用,判断变量指向的内存区域是否相同可以判断是否同一对象。
浅拷贝时la和lb的指向不同了,但内部的引用la[3]和lb[3]的指向仍是相同的。可见只复制了引用a[3]而没有复制引用指向的对象。
在深拷贝中a[3]和b[3]指向的内存是不一样的,可见复制了引用对象。
使用la[3] is lb[3]效果应该是一样的。
笔记-python-standard library-8.10 copy的更多相关文章
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- The Python Standard Library
The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...
- Python语言中对于json数据的编解码——Usage of json a Python standard library
一.概述 1.1 关于JSON数据格式 JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 46 ...
- 《The Python Standard Library》——http模块阅读笔记1
官方文档:https://docs.python.org/3.5/library/http.html 偷个懒,截图如下: 即,http客户端编程一般用urllib.request库(主要用于“在这复杂 ...
- 《The Python Standard Library》——http模块阅读笔记3
http.cookies — HTTP state management http.cookies模块定义了一系列类来抽象cookies这个概念,一个HTTP状态管理机制.该模块支持string-on ...
- 《The Python Standard Library》——http模块阅读笔记2
http.server是用来构建HTTP服务器(web服务器)的模块,定义了许多相关的类. 创建及运行服务器的代码一般为: def run(server_class=HTTPServer, handl ...
- Python Standard Library 学习(一) -- Built-in Functions 内建函数
内建函数列表 Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() ...
- [译]The Python Tutorial#10. Brief Tour of the Standard Library
[译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...
- C++11新特性——The C++ standard library, 2nd Edition 笔记(一)
前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...
- [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II
[译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...
随机推荐
- CentOS 6.2安装nagios
nagios分为监控机和被监控机两类机器 监控主机:gserver150(192.168.111.150) 被监控主机:gserver151(192.168.111.151) 一. 监 ...
- ifream页面弹出框遮盖层覆盖父页面
1.首先找到子页面上遮罩层的id, 2.然后再父页面编写个js方法 function shade() { $(".layui-layer-shade").height($(wind ...
- Canny边缘检测学习
Canny边缘检测学习:http://www.open-open.com/lib/view/open1453460512558.html 高斯滤波学习:http://www.cnblogs.com/q ...
- 页面中引入百度地图,实例化后影响html5的表单元素date的上下箭头
复现步骤: 使用百度地图的JavaScript的API,引入文件地址"http://api.map.baidu.com/api?key=&v=1.1&services=tru ...
- 常用CSS3属性整理
常用CSS3属性整理 文本 文本超出部分折叠 white-space:nowarp; overflow:hidden; text-overflow:ellipsis word-warp 边界换行 no ...
- Pixel XL 刷机及Root详细方法
需要准备的文件: 获取 Google USB 驱动程序并安装 https://developer.android.com/studio/run/win-usb 下载Google官方镜像文件 [8.1. ...
- 如何查看与显示oracle表的分区信息
显示分区表信息 显示数据库所有分区表的信息:DBA_PART_TABLES 显示当前用户可访问的所有分区表信息:ALL_PART_TABLES 显示当前用户所有分区表的信息:USER_PART_TAB ...
- 转 zigbee学习笔记---Channel、PANID、发射功率及其它参数
现对z-stack里几个网络参数的设置以及如何获取总结一下.信道配置:Zigbee在3个频段定义了27个物理信道:868MHz频段中定义了1个20Kb/s信道,915MHz频段中定义了10个40Kb/ ...
- C++之RAII惯用法
http://blog.csdn.net/hunter8777/article/details/6327704 C++中的RAII全称是“Resource acquisition is initial ...
- 第八章 熟练dom的几个常用方法
显示“缩略词语” <abbr> 标签指示简称或缩写,比如 <abbr title="World Wide Web Consortium">W3C</a ...