Copying

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

时间: 2024-09-30 19:34:12

Copying的相关文章

uva 714 Copying Books (二分)

uva 714 Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its co

Copying lists

When you assign an object to a variable, Python copies the reference to the object. In this case a and b refer to the same list. If you want to copy the list (not just a reference to it), you can use the slice operator: Making a slice of a creates a

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

(一) 上一条款说的auto_ptr和tr1::share_ptr适合于heap-based的资源,然而并不是所有资源都是heap-based的.换句话说并不是tr1::shared_ptr 和 auto_ptr 永远适合做为资源的管理者.所以有时难免还是需要实现自己的资源管理类型. 假设Mutex类型通过lock和unlock两组函数进行互斥器的锁定和解锁,可能我们希望和auto_ptr一样的行为,在某个智能类型析构时主动调用unlock进行解锁.比如下面的代码: void lock(Mute

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

Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come

当自己编写copying函数的时候要负责手动将每一个成员都拷贝

结论1:如果自己编写copying函数则编译器就不会再为你自动生成,如果新追加一个成员变量,但是忘记在copying函数中追加这个变量的拷贝行为,这样编译器不会提示任何错误和警告信息,但是新变量不会被拷贝.因此如果追加新的成员变量,必须记住手动为该成员变量实现copying操作. 代码段1.1:Customer.h文件 #ifndef CUSTOMER_H #define CUSTOMER_H #include <string> #include "Date.h" clas

uva 714 - Copying Books(贪心 最大值最小化 二分)

题目描述开头一大堆屁话,我还仔细看了半天..其实就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,如果最小值相同情况下有多种分配方案,输出前面份数小的,就像字典序输出从小到大一样的意思. 这里用到贪心的方法,定义f(x)为真的条件是满足x为最大值使n本书分成k份,那么就是求x的最小值.如何确定这个x就是用的二分法,x一定大于0小于所有值的合,不断的二分再判断是否成立,成立就取左半边,不成立说明太小了就取右半边,写的时候还是没有把二分法理解透彻,我还怕会丢失

Effective C++:条款14:在中小企业资源管理copying表现

(一) 在一项条款说法auto_ptr和tr1::share_ptr适合heap-based资源.然而,并非所有的资源都heap-based的.换句话说不tr1::shared_ptr 和 auto_ptr 总是适合作为资源管理器.管理类型. 如果Mutex类型通过lock和unlock两组函数进行相互排斥器的锁定和解锁,可能我们希望和auto_ptr一样的行为.在某个智能类型析构时主动调用unlock进行解锁. 比方以下的代码: void lock(Mutex* pm); void unloc

Solution of Publishing failed with multiple errors Error copying file static\

1.前言 由于系统被IT打了防病毒补丁,然后启动web项目一直出现Publishing failed with multiple errors Error copying file static...的错误,经过各种尝试包括重启电脑,新建工程,权限修改等都还是一样的错误提醒. 2.现象 错误信息如下: 3.解决方案 启动Eclipse时,右键用管理员权限打开,然后运行项目,问题解决了. 4.环境信息 OS: Win7 X64 Eclipse Version: Neon Release (4.6.

iOS9UICollectionView自定义布局modifying attributes returned by UICollectionViewFlowLayout without copying them

UICollectionViewFlowLayout has cached frame mismatch This is likely occurring because the flow layout subclass MyLineLayout is modifying attributes returned by UICollectionViewFlowLayout without copying them - (NSArray *)layoutAttributesForElementsIn