Shallow vs. deep copy python (9)

 

 

    What is shallow copy / deep copy, to put it bluntly, is actually a copy of the data, both in the end what difference does it make? Listening to quite confused, python development projects when maybe you'll run into this pit ~ ~

 
python
 

A. Ordinary variable assignment

    We normally use variable assignment is shallow copy, i.e. two variables share the same memory block, the same memory address, once the value changes, the value of another variable will change with follow demonstrate code is as follows:

= List1 [1,2,3,4,5 ]
 # universal variable assignment 
list2 = List1
 Print (ID (List1))
 Print (ID (list2)) 
 
# modify the data list list2 
list2.append (123 )
 Print (List1 )
 Print (list2)

 

 

Output:

2251297055368
2251297055368
[1, 2, 3, 4, 5, 123]
[1, 2, 3, 4, 5, 123]

 

    Note: a conventional variable assignment shared memory block, the same memory address, once the value changes, share the same memory address will change all the values ​​of the variables, the memory address may be a direct comparison can be built-in function ID ()!

 

Oh yo

 

Two shallow vs. deep copy

    In python development process, sometimes in the case of the above is not what we want, we want to be more variable assignment after making changes does not affect the value of the original variable, and how to achieve it? Here it is necessary to introduce the copy module:

    copy.copy() – 浅拷贝,重新分配内存,只拷贝父对象,不会拷贝对象的内部的子对象;

    copy.deepcopy() – 深拷贝,重新分配内存,拷贝对象及其所有子对象;

Copy shallow / deep copy

    1.浅拷贝copy()

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): shuopython.com
@WeChat Official Account(微信公众号):猿说python
@Github:www.github.com
 
@File:python_copy.py
@Time:2019/10/27:25
 
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
 
import copy
spam = ['A', 'B', 'C', 'D']
# 使用浅拷贝
cheese = copy.copy(spam)
cheese[1] = 42
print(id(spam),spam)
print(id(cheese),cheese)

 

输出结果:

57205555 ['A', 'B', 'C', 'D']
57208888 ['A', 42, 'C', 'D']

 

 

    2.深拷贝deepcopy()

import copy
spam = ['A', 'B', 'C', 'D']
# 使用深拷贝
cheese = copy.deepcopy(spam)
cheese[1] = 42
print(id(spam),spam)
print(id(cheese),cheese)

 

 

输出结果:

57205555 ['A', 'B', 'C', 'D']
57208888 ['A', 42, 'C', 'D']

 

 

    3.浅拷贝和深拷贝区别

    对于常规的字典或者列表使用copy模块的深拷贝或者浅拷贝,两者并没有区别!如果字典或者列表中还有包含有子类的话,使用copy模块的深拷贝和浅拷贝的话,结果就大不相同了:

    copy.copy() — 重新分配内存,只拷贝父对象,不会拷贝对象内部的子对象;

    copy.deepcopy() — 重新分配内存,拷贝对象及其所有子对象;

    示例代码如下:

import copy
 
print("使用浅拷贝:")
spam = [['A','E'], 'B', 'C', 'D']
# 使用浅拷贝
cheese = copy.copy(spam)
cheese[0][0] = 42
print(id(spam),spam)
print(id(cheese),cheese)
print("***"*20)
 
print("使用深拷贝:")
spam = [['A','E'], 'B', 'C', 'D']
# 使用深拷贝
cheese = copy.deepcopy(spam)
cheese[0][0] = 42
print(id(spam),spam)
print(id(cheese),cheese)

 

 

 

输出结果:

使用浅拷贝:
2179653046408 [[42, 'E'], 'B', 'C', 'D']
2179653046920 [[42, 'E'], 'B', 'C', 'D']
************************************************************
使用深拷贝:
2179653086728 [['A', 'E'], 'B', 'C', 'D']
2179653046408 [[42, 'E'], 'B', 'C', 'D']

 

    This shows that:

    If the dictionary does not contain the list or sub-list or sub-dictionary words, or using a deep copy shallow copy has the same effect;

    If, when there is a subclass of list or dictionary, only a deep copy will be for all subclasses also re-allocate memory, and shallow copy only responsible parent, regardless of the child object! !

ear

 

you may also like:

    1.python list comprehensions

    2.python dictionary derivations

    3.python return logic operation

    4.python thread creation

    5.python anonymous function lambda

 

    Reproduced please specify: ape say Python  »  Python shallow vs. deep copy

 

Technical exchanges, business cooperation please contact bloggers
Scan code or search: ape say python
No public python tutorial
Ape say python
No. sweep the micro-channel public concern

Guess you like

Origin www.cnblogs.com/shuopython/p/12048582.html