码迷,mamicode.com
首页 > 其他好文 > 详细

Copying

时间:2014-09-30 02:01:51      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   ar   for   sp   2014   

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:

bubuko.com,布布扣                       

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.

bubuko.com,布布扣 

Here is what the object diagram looks like:

bubuko.com,布布扣 

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.

bubuko.com,布布扣 

box3 and box are completely separate objects.

 

from Thinking in Python

Copying

标签:style   blog   http   io   os   ar   for   sp   2014   

原文地址:http://www.cnblogs.com/ryansunyu/p/4001199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!