Oh, my God! Only 5 pictures, thoroughly understand the depth of Python copy

First day of junior high school 2020-11-13 17:31:47
oh god pictures thoroughly understand



Python Deep and shallow copy of

Before we talk about depth copy , Let's review is and == The difference between .

In judging whether the objects are equal or not, we can use is and ==

  • is: Compare two objects for the same reference , namely Their id Whether or not the same
  • == : Compare two objects for the same value .

id() , yes Python A built-in function , Returns the unique identification of the object , To get the memory address of the object .

as follows

First , Will be an integer 1 Allocate a memory space . Variable a and b All point to this memory space ( Memory addresses are equal ), So their id equal .

namely a is b by True

however , Is that true for all integers ? The answer is : No ! Only in -25 ~ 256 Integers in the range do not reallocate memory space .

As shown below :

because 257 Out of range , therefore id inequality , therefore a is b The returned value is False.

>>> a = 257
>>> b = 257
>>> print(id(a))
20004752
>>> print(id(b))
20001312
>>> print(a is b)
False
>>> print(a == b)
True

This is done with performance in mind ,Python Yes -5 To 256 The integer of maintains an array , It's equivalent to a cache , When the value is in this range , The corresponding reference address is returned directly from the array . If it's not in this range , Will open up a new memory space .

is and == Which is more efficient ?

by comparison ,is Comparison is more efficient , Because it only needs to judge the id Whether it is the same or not .

and == Overload is required __eq__ This function , Traverse the contents of all elements in a variable , Is the comparison the same . So it's inefficient

Shallow copy Deep copy

Assign values to variables , There are two ways Direct assignment , Copy

Direct assignment is just = That's all right. . Copy is divided into shallow copy and deep copy

Let's start with the conclusion :

  • Shallow copy : The copy is a reference to the object , If the original object changes , The corresponding copy object will also change
  • Deep copy : Copy every element in the object , The copy object is no longer related to the original object , Two are separate objects

Just look at the concept above , It may not be easy for beginners to understand . Take a look at the following example

assignment

a = [1, 2, 3]
b = a
print(id(a)) # 52531048
print(id(b)) # 52531048

Defining variables a, At the same time a Assign a value to b. After printing, they found their id It's the same . The description points to the same memory address .

And then modify a Value , Check out their id

a = [1, 2, 3]
b = a
print(id(a)) # 46169960
a[1] = 0
print(a, b) # [1, 0, 3] [1, 0, 3]
print(id(a)) # 46169960
print(id(b)) # 46169960

At this time, I found the modified a and b And the beginning a The memory address is the same . in other words a and b Still pointing to that piece of memory , It's just in memory [1, 2, 3] Turned into [1, 0, 3]

Because the memory address changes every time it is executed again , At this time id(a) Value 46169960 And 52531048 It's the same

So we can judge ,b and a The quotation of is the same , When a When things change ,b It will change .

Assignment is : you a No matter how , Who are you pointing to , I b Follow who you point to .

Copy

When it comes to copying, you can't avoid mutable and immutable objects .

  • The variable object : When it is necessary to change the values inside the object , Of this object id No change .

  • Immutable object : When it is necessary to change the values inside the object , Of this object id Will change .

a = [1, 2, 3]
print(id(a)) # 56082504
a.append(4)
# Modify the list a after id It hasn't changed , The variable object
print(id(a)) # 56082504
a = 'hello'
print(id(a)) # 59817760
a = a + ' world'
print(id(a)) # 57880072
# Modify string a after ,id There is a change . Immutable object
print(a) # hello world

Shallow copy

Copy the immutable object , To some extent, it's the same as assignment operation . But for multi-level nested structures , Shallow copy copies only the parent object , Do not copy internal child objects .

Use copy Modular copy.copy Make a light copy .

import copy
a = [1, 2, 3]
b = copy.copy(a)
print(id(a)) # 55755880
print(id(b)) # 55737992
a[1] = 0
print(a, b) # [1, 0, 3] [1, 2, 3]

Generally speaking , I will now a Copy a copy and reallocate a memory space . Behind you a How to change , Then follow me b It doesn't matter .

For shallow copies of lists, you can also use list(), list[:] To achieve

however ! I mentioned earlier that for multi nested structures , We need to pay attention to

See the following example

import copy
a = [1, 2, [3, 4]]
b = copy.copy(a)
print(id(a)) # 23967528
print(id(b)) # 21738984
# change a Sub list in
a[-1].append(5)
print(a) # [1, 2, [3, 4, 5]]
print(b) # [1, 2, [3, 4, 5]] ?? Why not [1, 2, [3, 4]] Well ?

b By a A shallow copy of . I modified a Nested list in , Find out b It has also been revised ?

If you still don't understand , Refer to the figure below .LIST It's a nested sub object , Points to another memory space . So shallow copy just copies the elements 1, 2 And child object references !

Another situation , What if a tuple is nested ?

import copy
a = [1, 2, (3, 4)]
b = copy.copy(a)
# change a Tuples in
a[-1] += (5,)
print(a) # [1, 2, (3, 4, 5)]
print(b) # [1, 2, (3, 4)]

We found that the light copy came from b Nothing has changed . Because tuples are immutable objects . Changing the tuple will generate a new object .b The tuple reference in still points to the old tuple .

Deep copy

What about deep copy , Is to reallocate a memory space ( New object ), Copy all the elements in the original object recursively to the new object .

stay Python in adopt copy.deepcopy() To achieve deep copy .

import copy
a = [1, 2, [3, 4]]
b = copy.deepcopy(a)
print(id(a)) # 66587176
print(id(b)) # 66587688
# change a Variable objects in
a[-1].append(5)
print(a) # [1, 2, [3, 4, 5]]
print(b) # [1, 2, [3, 4]] After deep copy, the word list will not be affected by the original

Conclusion

1、 Both deep and shallow copies copy the source object , Take up different memory space

2、 If the source object has no subdirectories , The shallow copy can only copy the parent directory , Changing subdirectories affects shallow copy objects

3、 Slicing a list is essentially a shallow copy


In the history of the most complete Python Data summary ( Long term renewal ). The children next door were crying --- Click to collect

版权声明
本文为[First day of junior high school]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database