Aliasing can make program difficult to read because changes made in one place might have unexpected effects in another place. It is hard to keep track of all variables that might refer to a given object.

Copying an object is often an alternative to aliasing. The copy module contains a function called copy that can duplicate any object:

p1 and p2 contain the same data, but they are not the same Point. The is operator indicates that p1 and p2 are not the same object, which is what we expected. But you might have expected == to yield True because these points contain the same data. In that case, you will be disappointed to learn that for instances, the default behavior of the == operator is the same as the is operator; it checks object identity, not object equivalence. This behavior can be changed, so for many objects defined in Python modules, the == operator checks equivalence (in whatever sense is appropriate). But the default is to check identity. If you use copy.copy to duplicate a Rectangle, you will find that it copies the Rectangle object but not the embedded Point.

Here is what the object diagram looks like:

This operation is called a shallow copy because it copies the object and any references it contains, but not the embedded objects.

For most applications, this is not what you want. In this examples, invoking grow_rectangle on one of the Rectangles would not affect the other, but invoking move_rectangle on either would affect both! This behavior is confusing and errorprone. Fortunately, the copy module contains a method named deepcopy that copies not only the object but also the objects it refers to, and the objects they refer to, and so on. You will not be surprised to learn that this operation is called a deep copy.

box3 and box are completely separate objects.

from Thinking in Python

Copying的更多相关文章

  1. 14——小心copying行为

    资源的copying行为决定对象的copying行为. 抑制copying行为,使用引用计数.

  2. UVa 714 Copying Books(二分)

    题目链接: 传送门 Copying Books Time Limit: 3000MS     Memory Limit: 32768 KB Description Before the inventi ...

  3. Effective C++ -----条款14: 在资源管理类中小心copying行为

    复制RAII对象必须一并复制它所管理的资源,所以资源的copying行为决定RAII对象的copying行为. 普遍而常见的RAII class copying行为是:抑制copying(使用私有继承 ...

  4. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

  5. 抄书 Copying Books UVa 714

    Copying  Books 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B 题目: Descri ...

  6. Copying Fields to a new Record

    This is a time saving tip for application designer. If you are creating a new record definition and ...

  7. UVA 714 Copying Books 二分

    题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...

  8. poj 1505 Copying Books

    http://poj.org/problem?id=1505 Copying Books Time Limit: 3000MS   Memory Limit: 10000K Total Submiss ...

  9. [Effective C++ --014]在资源管理类中小心copying行为

    第一节 <背景> 条款13中讲到“资源取得的时机便是初始化时机”并由此引出“以对象管理资源”的概念.通常情况下使用std中的auto_ptr(智能指针)和tr1::shared_ptr(引 ...

  10. Copying Linked Lists with Random Pointers

    Copying Linked Lists with Random Pointers 两个方法:方法一: 1.不考虑随机指针的情况下复制链表: 2.在复制过程中建立一个以原链表节点地址为key,相应的复 ...

随机推荐

  1. 关于Javakeywordsynchronized——单例模式的思考

    精彩的设计模式盛宴刚刚落下帷幕.三天的时间.真是学习到了非常多.当中,遗留的非常多的问题.今天就谈谈synchronized这个keyword.关于对synchronizedkeyword的思考是从单 ...

  2. 试用友盟SDK实现Android分享微信朋友圈

    社会化分享是眼下必学且火热的功能.之前有写第三方登录,那仅仅是社会化分享的一部分.今天来玩玩分享微信朋友圈. 为了方便操作,还是依照步骤写. 一,注冊 注冊应用已经在这里具体说明过了,这里就不多提了. ...

  3. C#post调用接口并上传文件

    /// <summary> /// C#调用接口上传json数据,并且带文件上传 /// </summary> /// <param name="url&quo ...

  4. C#的常见算法(面试)(转)

    一.求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m //方法一,通过顺序规律写程序,同时也知道flag标志位的重要性. static int F1(int m) { ; ...

  5. hdu 6082 - 完全背包,打表。

    2017-08-06 15:02:50 Accepted 1003 187 MS 2168 K G++ redips 对背包有了进一步的认识 ----------------------------- ...

  6. STM8S103汇编文档和注意

    1.官方文档<STM8 CPU programming manual>介绍了指令和寻址方式 2.辅助类文档,boot loader文档<STM8 bootloader> 3.关 ...

  7. HDU 1548 A strange lift【BFS】

    题意:给出一个电梯,给出它的层数f,给出起点s,终点g,以及在每一层能够上或者下w[i]层,问至少需要按多少次按钮到达终点. 和POJ catch that cow一样,直接用了那一题的代码,发现一直 ...

  8. 高阶函数-lambda表达式

    #2.6 map()# 第一个参数传入一个函数,,第二个参数为一个可迭代对象li_1 = (1,3,5,7)def funcA(x): return x*xm1 = map(funcA,li_1)pr ...

  9. vue v-if的使用

    代码部分 <el-row> <el-col :span="20"> <template v-for="(node,i) of hierarc ...

  10. python自动化报错

    今天使用python.然而遇见了报错.抓狂的一笔.有说path写错的,有说是...网上查到的资料也是很少.后来突然发现,页面上我暂时能看到的元素可以定位并进行操作.看不到的无法进行...ps此时我没有 ...