copy module
需求,当有一个实例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的更多相关文章
- Shallow copy and Deep copy
Shallow copy and Deep copy 第一部分: 一.来自wikipidia的解释: Shallow copy One method of copying an object is t ...
- grunt配置太复杂?使用Qbuild进行文件合并、压缩、格式化等处理
上次简单介绍了下Qbuild的特点和配置,其实实现一个自动化工具并不复杂,往简单里说,无非就是筛选文件和处理文件.但Qbuild的源码也并不少,还是做了不少工作的. 1. 引入了插件机制.在Qbuil ...
- grunt配置太复杂?发布一个前端构建工具,简单高效,自动跳过未更新的文件
做前端项目,如果没有一个自动化构建工具,手动处理那简直就是坑爹O(∩_∩)O.于是上网了解了下,grunt用的人不少,功能也挺强大.看了一下grunt的配置(包括gulp),感觉稍显复杂.当时项目结构 ...
- metasploit-post模块信息
Name Disclosure Date Rank Description ---- ...
- 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 ...
- 开源图形库 c语言-图形图像库 集合[转]
开源图形库 c语言-图形图像库 集合[转] Google三维API O3D O3D 是一个开源的 Web API 用来在浏览器上创建界面丰富的交互式的 3D 应用程序.这是一种基于网页的可控3D标准. ...
- Think Python Glossary
一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...
- Angular源代码学习笔记-原创
时间:2014年12月15日 14:15:10 /** * @license AngularJS v1.3.0-beta.15 * (c) 2010-2014 Google, Inc. http:// ...
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(八)
如无书面授权,请勿转载 第四章,大型项目中Ansible的使用 Roles If your playbooks start expanding beyond what includes can hel ...
随机推荐
- Zookeeper 3.4.6 Client端流程粗略梳理
首先从Zookeeper入手,Zookeeper-->ClientCnxn-->sendThread/eventThread public ZooKeeper(String connect ...
- python 代码片段20
#coding=utf-8 # 函数 def foo(x): print x foo(123) # import httplib def check_web_server(host,port,path ...
- Android js相互调用
一.webview相当于android中的浏览器,基于webkit开发,可以浏览网页文件,支持css javas cript 以及html webview.getSettings().setJavaS ...
- ACM Color the fence
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- 【BZOJ】3456: 城市规划
http://www.lydsy.com/JudgeOnline/problem.php?id=3456 题意:求n个点的无向连通图的方案.(n<=130000) #include <bi ...
- 【JAVA】JMX简单使用方法
[BEAN] 配置 <!-- JMX 对应的接口服务--> <bean id="emailInterfaceServer" class="com.s ...
- 简单 常用的git命令
常用的git命令 git pull 获取最新 git add . 提交所有 git commit -m “我的注释” git status 查看状态 git push origin master ...
- Linux分区练习(1)
1.作业描述: 4个主分区. 具体实现过程: 打开Linux,在终端中输入:fdisk -uc /dev/sda 可以查看到 :Command (m for hel ...
- 20145330第六周《Java学习笔记》
20145330第六周<Java学习笔记> . 这周算是很忙碌的一周.因为第六周陆续很多实验都开始进行,开始要准备和预习的科目日渐增多,对Java分配的时间不知不觉就减少了,然而第十和十一 ...
- CentOS转的服务器磁盘规划
我的服务器是500G.最重要的是/var分区一定要大(不论postfix邮件,还是LAMP的WEB 服务器等).最好是400G以上.具体的/boot 只要100M就足够了.下面是我的分区方案:硬盘50 ...