copy 浅拷贝 深拷贝




浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象
深拷贝 拷贝对象及其子对象


1
#直接赋值和 copy 的区别 2 dic1={'name': 'Tom', 'age': 18, 'hobby': 'running'} 3 dic2=dic1 4 5 print(dic1) 6 print(dic2) 7 8 #修改数据 age=28 9 dic1['age']=28 10 11 print(dic1) 12 print(dic2) 13 # 浅拷贝: 引用对象 dic2只是对dic1的引用。所以当dic1的值改变,dic2也会随着改变 14 15 16 dic3={'name': 'Tom', 'age': 18, 'hobby': 'running'} 17 18 dic4=dic3.copy() 19 #父对象(一级目录,直接指向最终的值),子对象(二级目录)引用地址,中间有一层地址存储变量作为中介 20 print(dic3) 21 print(dic4) 22 #修改数据 age=38 23 dic3['age']=38 24 25 print(dic3) 26 print(dic4) 27 28 dic5={'name': ['Tom','Jack'], 'age':[ 18,28], 'hobby': ['running','football']} 29 30 dic6=dic5.copy() 31 32 #父对象(一级目录),子对象(二级目录)不拷贝,还是引用 33 print(dic5) 34 print(dic6) 35 #修改一级目录(父对象),修改数据 ['running','football']修改为['basketball'] 36 dic5['hobby']=['basketball',] 37 38 #修改二级目录(子对象),修改数据 Tom 改成 May 39 dic5['name'][0]='May' 40 dic5['age'][0]=38 41 42 print(dic5) 43 print(dic6)

结果:

{'name': 'Tom', 'age': 18, 'hobby': 'running'}
{'name': 'Tom', 'age': 18, 'hobby': 'running'}

{'name': 'Tom', 'age': 28, 'hobby': 'running'}
{'name': 'Tom', 'age': 28, 'hobby': 'running'}

{'name': 'Tom', 'age': 18, 'hobby': 'running'}
{'name': 'Tom', 'age': 18, 'hobby': 'running'}

{'name': 'Tom', 'age': 38, 'hobby': 'running'}
{'name': 'Tom', 'age': 18, 'hobby': 'running'}

{'name': ['Tom', 'Jack'], 'age': [18, 28], 'hobby': ['running', 'football']}
{'name': ['Tom', 'Jack'], 'age': [18, 28], 'hobby': ['running', 'football']}

{'name': ['May', 'Jack'], 'age': [38, 28], 'hobby': ['basketball']}
{'name': ['May', 'Jack'], 'age': [38, 28], 'hobby': ['running', 'football']}
#深入理解浅拷贝
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> a=[[1,2],3,4] >>> b=a.copy() >>> b [[1, 2], 3, 4] >>> b[0][1]=5 >>> b [[1, 5], 3, 4] >>> a [[1, 5], 3, 4] >>> b[1]=3.5 >>> b [[1, 5], 3.5, 4] >>> a [[1, 5], 3, 4] >>> id(a) 48899400 >>> id(a[0]) 48899464 >>> id(a[1]) SyntaxError: unexpected indent >>> id(a[1]) 1346203168 >>> id(a[2]) 1346203200 >>> id(a[0][0]) 1346203104 >>> >>> id(a[0][1]) 1346203232 >>> a [[1, 5], 3, 4] >>> b=a.copy() >>> b [[1, 5], 3, 4] >>> id(b) 49052488 >>> id(b[0]) 48899464 >>> id(b[1]) 1346203168 >>> id(b[2]) 1346203200 >>> id(b[0][0]) 1346203104 >>> id(b[0][1]) 1346203232 >>> b[1]=3.1 >>> b [[1, 5], 3.1, 4] >>> a [[1, 5], 3, 4] >>> id(b[1]) 30157440 >>> b[0][1]=5.1 >>> b [[1, 5.1], 3.1, 4] >>> a [[1, 5.1], 3, 4] >>> id(b[0][1]) 30155760 >>> id(a[0][1]) 30155760 >>> id(a[1]) 1346203168 >>> a[0][0]=1.2 >>> a [[1.2, 5.1], 3, 4] >>> b [[1.2, 5.1], 3.1, 4] >>> id(a[0][0]) 30156240 >>> id(b[0][0]) 30156240 >>> id(b[0]) 48899464 >>> id(a[0]) 48899464 >>>

 

 

import copy

print(dir(copy))
print(copy.__doc__)
['Error', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_copy_dispatch', '_copy_immutable', '_deepcopy_atomic', '_deepcopy_dict', '_deepcopy_dispatch', '_deepcopy_list', '_deepcopy_method', '_deepcopy_tuple', '_keep_alive', '_reconstruct', 'copy', 'deepcopy', 'dispatch_table', 'error']
Generic (shallow and deep) copying operations.

Interface summary:

        import copy

        x = copy.copy(y)        # make a shallow copy of y
        x = copy.deepcopy(y)    # make a deep copy of y

For module specific errors, copy.Error is raised.

The difference between shallow and deep copying is only relevant for
compound objects (objects that contain other objects, like lists or
class instances).

- A shallow copy constructs a new compound object and then (to the
  extent possible) inserts *the same objects* into it that the
  original contains.

- A deep copy constructs a new compound object and then, recursively,
  inserts *copies* into it of the objects found in the original.

Two problems often exist with deep copy operations that don't exist
with shallow copy operations:

 a) recursive objects (compound objects that, directly or indirectly,
    contain a reference to themselves) may cause a recursive loop

 b) because deep copy copies *everything* it may copy too much, e.g.
    administrative data structures that should be shared even between
    copies

Python's deep copy operation avoids these problems by:

 a) keeping a table of objects already copied during the current
    copying pass

 b) letting user-defined classes override the copying operation or the
    set of components copied

This version does not copy types like module, class, function, method,
nor stack trace, stack frame, nor file, socket, window, nor array, nor
any similar types.

Classes can use the same interfaces to control copying that they use
to control pickling: they can define methods called __getinitargs__(),
__getstate__() and __setstate__().  See the documentation for module
"pickle" for information on these methods.


Process finished with exit code 0

 

posted @ 2018-06-06 16:56  巨兽~墨菲特  阅读(245)  评论(0编辑  收藏  举报