需求,当有一个实例a,我们需要一个新的实例b,b同a拥有相同的属性。

当我们使用a=b的模式的时候是一个赋值的过程。a和b指向同一个实例。b的任何操作都同a一样。

在这个使用需要使用copy模块。根据a copy出一个一模一样的b。

class shelf(object):
def __init__(self):
self.books = [] def addbook(self, bookname):
self.books.append(bookname) def delbook(self, bookname):
self.books.remove(bookname) def showbook(self):
for book in self.books:
print book shelf1 = shelf()
shelf1.addbook("the great gatsby")
shelf1.addbook("the little prince")
shelf1.showbook()
print str(id(shelf1))
print "------------0--------------"
import copy
shelf2 = copy.copy(shelf1)
shelf2.showbook()
print str(id(shelf2))
print "------------1---------------"

结果:

the great gatsby
the little prince
31398768
------------0--------------
the great gatsby
the little prince
40185472
------------1---------------

可以看出shelf1和shelf2是两个实例,但是有着相同的属性。

但是有一个问题:

shelf2.delbook("the little prince")
shelf2.showbook()
print "-------------2--------------"
shelf1.showbook()

结果呢?

the great gatsby
-------------2--------------
the great gatsby

  这说明虽然shelf1和shelf2不同的类,但是内容仍然指向相同的地点。

如何解决这个问题:

copy.deepcopy

看代码:

shelf3 = copy.deepcopy(shelf1)
print "------------3---------------"
shelf3.showbook()
shelf3.addbook("The Wonderful Wizard of Oz")
print "-------------4--------------"
shelf3.showbook()
print "--------------5-------------"
shelf1.showbook()

  结果:

the great gatsby
-------------4--------------
the great gatsby
The Wonderful Wizard of Oz
--------------5-------------
the great gatsby

  这样就解决了内容指向相同的问题。

所以copy模块中copy函数和deepcopy函数的区别就是当类内部有list,dict时候,copy产生的实例有着指向相同内容,deepcopy则将list/dict也创建一个备份。

完整代码:

# -*- coding: utf-8 -*-

class shelf(object):
def __init__(self):
self.books = [] def addbook(self, bookname):
self.books.append(bookname) def delbook(self, bookname):
self.books.remove(bookname) def showbook(self):
for book in self.books:
print book shelf1 = shelf()
shelf1.addbook("the great gatsby")
shelf1.addbook("the little prince")
shelf1.showbook()
print str(id(shelf1))
print "------------0--------------"
import copy
shelf2 = copy.copy(shelf1)
shelf2.showbook()
print str(id(shelf2))
print "------------1---------------"
shelf2.delbook("the little prince")
shelf2.showbook()
print "-------------2--------------"
shelf1.showbook() shelf3 = copy.deepcopy(shelf1)
print "------------3---------------"
shelf3.showbook()
shelf3.addbook("The Wonderful Wizard of Oz")
print "-------------4--------------"
shelf3.showbook()
print "--------------5-------------"
shelf1.showbook()

  

copy module的更多相关文章

  1. Shallow copy and Deep copy

    Shallow copy and Deep copy 第一部分: 一.来自wikipidia的解释: Shallow copy One method of copying an object is t ...

  2. grunt配置太复杂?使用Qbuild进行文件合并、压缩、格式化等处理

    上次简单介绍了下Qbuild的特点和配置,其实实现一个自动化工具并不复杂,往简单里说,无非就是筛选文件和处理文件.但Qbuild的源码也并不少,还是做了不少工作的. 1. 引入了插件机制.在Qbuil ...

  3. grunt配置太复杂?发布一个前端构建工具,简单高效,自动跳过未更新的文件

    做前端项目,如果没有一个自动化构建工具,手动处理那简直就是坑爹O(∩_∩)O.于是上网了解了下,grunt用的人不少,功能也挺强大.看了一下grunt的配置(包括gulp),感觉稍显复杂.当时项目结构 ...

  4. metasploit-post模块信息

    Name                                             Disclosure Date  Rank    Description ----           ...

  5. Think Python - Chapter 15 - Classes and objects

    15.1 User-defined typesWe have used many of Python’s built-in types; now we are going to define a ne ...

  6. 开源图形库 c语言-图形图像库 集合[转]

    开源图形库 c语言-图形图像库 集合[转] Google三维API O3D O3D 是一个开源的 Web API 用来在浏览器上创建界面丰富的交互式的 3D 应用程序.这是一种基于网页的可控3D标准. ...

  7. Think Python Glossary

    一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...

  8. Angular源代码学习笔记-原创

    时间:2014年12月15日 14:15:10 /** * @license AngularJS v1.3.0-beta.15 * (c) 2010-2014 Google, Inc. http:// ...

  9. Ansible@一个高效的配置管理工具--Ansible configure management--翻译(八)

    如无书面授权,请勿转载 第四章,大型项目中Ansible的使用 Roles If your playbooks start expanding beyond what includes can hel ...

随机推荐

  1. appium运行报错.<init>(Lorg/openqa/selenium/remote/ErrorCodes;Z)V

    最近这几天就在学习appium,搭建环境就耗费了很多时间,不得不承认自己够笨的了,然后我把环境搭建好,写完脚本的时候,就报这个错了,当时是从某个群里直接下载的demo,不得不吐槽说,够坑的,是能跑通, ...

  2. Graphics 导出图片使用【这个主要是画图类图的使用,记录一下】

    /// <summary> /// 导出信令流程矢量图 /// </summary> /// <param name="signalFlowInfos" ...

  3. springmvc导出excel并弹出下载框

    https://my.oschina.net/aptx4869/blog/298507

  4. 贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know

    题目传送门 /* 题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大 贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记):2. 有偶数但都比末尾数字小(x位置标记) 仿照别人写的,再 ...

  5. 向量时钟Vector Clock in Riak

    Riak 是以 Erlang 编写的一个高度可扩展的分布式数据存储,Riak的实现是基于Amazon的Dynamo论文,Riak的设计目标之一就是高可用.Riak支持多节点构建的系统,每次读写请求不需 ...

  6. hdfs 集群间拷贝

    hadoop distcp -i hdfs://192.168.10.211:9000/fileinfo hdfs://192.168.24.46:9000/fileinfo distcp [OPTI ...

  7. BZOJ3764 : Petya的序列

    首先如果一段连续子序列里没有任何幸运数,那么显然可以缩成一个点. 设幸运数个数为$m$,那么现在序列长度是$O(m)$的,考虑暴力枚举$R_1$,然后从右往左枚举$L_1$. 每次碰到一个幸运数,就将 ...

  8. hadoop2.0安装中遇到的错误:mapreduce.shuffle set in yarn.nodemanager.aux-services is invalid

    转:http://blog.csdn.net/bamuta/article/details/12995139 解决办法 : 在1个网站上找到了解决方法,(网络忘了没记)urg, my copy/pas ...

  9. ubuntu下命令行禁用笔记本触摸板

    机房电脑不好用,所以用笔记本,但是由于笔记本过分紧凑手经常让鼠标不知道跑哪里去.于是找到了这两个命令 禁用:sudo rmmod psmouse 启用:sudo modprobe psmouse 非常 ...

  10. OI刷题记录

    从六月一号开始记录啦 6月1日 link-cut-tree BZOJ2631 tree