笔记-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 抛出模块特定错误

浅复制和深复制的区别:

  1. 两者之间的区别主要是针对复杂对象而言的,例如列表和类实例。
  2. 浅复制复制对象但不复制引用体。
  3. 深复制复制对象,同时递归的去寻找它在原始对象的内容并复制。

深复制通常会存在两个问题,而浅复制不会:

  1. 递归对象可能会导致递归循环;
  2. 深复制可能会复制太多不必要的内容,比如应该在两个对象之间共享的内容。

针对上述问题,深拷贝通过以下方式避免:

  1. 保留一个“memo”字典用于描述拷贝的对象。
  2. 让用户定义的类覆盖掉复制操作或复制过来的组件。

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的更多相关文章

  1. Python Standard Library

    Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...

  2. The Python Standard Library

    The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...

  3. Python语言中对于json数据的编解码——Usage of json a Python standard library

    一.概述 1.1 关于JSON数据格式 JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 46 ...

  4. 《The Python Standard Library》——http模块阅读笔记1

    官方文档:https://docs.python.org/3.5/library/http.html 偷个懒,截图如下: 即,http客户端编程一般用urllib.request库(主要用于“在这复杂 ...

  5. 《The Python Standard Library》——http模块阅读笔记3

    http.cookies — HTTP state management http.cookies模块定义了一系列类来抽象cookies这个概念,一个HTTP状态管理机制.该模块支持string-on ...

  6. 《The Python Standard Library》——http模块阅读笔记2

    http.server是用来构建HTTP服务器(web服务器)的模块,定义了许多相关的类. 创建及运行服务器的代码一般为: def run(server_class=HTTPServer, handl ...

  7. Python Standard Library 学习(一) -- Built-in Functions 内建函数

    内建函数列表 Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() ...

  8. [译]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模块为与操作系统 ...

  9. C++11新特性——The C++ standard library, 2nd Edition 笔记(一)

    前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...

  10. [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II

    [译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...

随机推荐

  1. jquery.validate+jquery.form表单验证提交

    1.通过jquery.validate的submitHandler选项,即当表单通过验证时运行回调函数.在这个回调函数中通过jquery.form来提交表单: <script type=&quo ...

  2. hibernate课程 初探单表映射1-6 hibernate项目建立以及导入jar包

    hibernate 项目建立 1 new ==>java project hibernate 导入jar包 1 windows==>prerence==>java ==>bui ...

  3. cf1040E. Network Safety(并查集)

    题意 题目链接 一张图,n个点,m条边,每个点有个权值x,x<=1e18.如果一条边的两个端点不一样,那么这条边是安全的,开始时所有边都是安全的. 现在有一个病毒y,病毒可以入侵任意的点,入侵一 ...

  4. 父类和子类以及super关键字

    super和this关键字的特点类似:super代表的是父类对象的引用. 当子父类的成员出现同名时,可以通过super来进行区分. 子类的构造方法中,通过super关键字调用父类的构造方法. publ ...

  5. python property用法

    参考 http://openhome.cc/Gossip/Python/Property.html http://pyiner.com/2014/03/09/Python-property.html ...

  6. Qt安装教程

    一.Qt下载 官网下载链接http://download.qt.io/archive/qt/,下载最新版 5.10 官网的下载网站有的时候可能会抽风,也可以选择国内的镜像下载源http://mirro ...

  7. PowerShell (407) Proxy Authentication Required

    $Client = New-Object -TypeName System.Net.WebClient $Client.Proxy.Credentials = [System.Net.Credenti ...

  8. vue安装环境

    vue安装环境 1. 安装node.js 先在电脑上安装node.js, https://nodejs.org/en/ 可以点击链接安装. 安装成功后,在命令板里检测是否安装成功, node -v 2 ...

  9. fread, fwrite - 二进制流的输入/输出

    总览 (SYNOPSIS) #include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stre ...

  10. Python实现屏蔽敏感词

    一.需求 1. 有一个文件,里面有一些敏感词汇,用户输入一段话,若包含这些词,就用**代替,并输出 二.实现代码 f = open('lib.txt', 'r') result = '' f1 = i ...