Python's shades of Copy

Source: Internet
Author: User
Tags shallow copy

Depth copy

For numbers and strings, assignments, shallow copies, and deep copies are meaningless because they always point to the same memory address.

import copy # ######### 数字、字符串 ######### n1  = 123 # n1 = "age 10" print ( id (n1)) # ## 赋值 ## n2  = n1

print(id(n2))


# ## 浅拷贝 ## n2  = copy.copy(n1) print ( id (n2)) # ## 深拷贝 ## n3  = copy.deepcopy(n1) print ( id (n3))


For dictionaries, tuples, and lists, the change in memory address is different when assigning, shallow copy, and deep copy.

assignment , just create a variable that points to the original memory address, such as:

>>> n1 = {"K1": "Zhangshan", "K2": 123, "K3": ["Lisi", 456]}

>>> N2 = N1

>>> Print ID (n1)

11195200

>>> Print ID (n2)

11195200

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/9A/EB/wKiom1lcROjjEh8xAAAXptnQHpo075.png-wh_500x0-wm_ 3-wmp_4-s_4110100440.png "title=" Qq20170705094413.png "alt=" Wkiom1lcrojjeh8xaaaxptnqhpo075.png-wh_50 "/>


Shallow Copy , only the first layer of data is created in memory

>>> n1 = {"K1": "Zhangshan", "K2": 123, "K3": ["Lisi", 456]}

>>> n3 = copy.copy (N1)

>>> Print ID (n1)

11267952

>>> Print ID (n3)

11151792

>>> Print ID (n1[' K3 ') # #查看内存地址相同

139853825328696

>>> Print ID (n3[' K3 ') # #查看内存地址相同

139853825328696


650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/9A/EB/wKiom1lcR1bxJO5bAAAojoI_Hfw379.png-wh_500x0-wm_ 3-wmp_4-s_455700749.png "title=" qq picture 20170705095448.png "alt=" Wkiom1lcr1bxjo5baaaojoi_hfw379.png-wh_50 "/>

Deep Copy , recreate all the data in memory (excluding the last layer, that is, Python's internal optimization of strings and numbers)

>>> Import Copy

>>> n1 = {"K1": "Zhangshan", "K2": 123, "K3": ["Lisi", 456]}

>>> N4 = copy.deepcopy (n1)

>>> Print ID (n1)

11193264

>>> Print ID (n4)

11249664

>>> Print ID (n1[' K3 ') # #查看内存地址不同

139853635849824

>>> Print ID (n2[' K3 ') # #查看内存地址不同

139853635827256

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/9A/EC/wKioL1lcSfuTYRLDAAAojoI_Hfw422.png-wh_500x0-wm_ 3-wmp_4-s_3427698412.png "title=" qq picture 20170705095448.png "alt=" Wkiol1lcsfutyrldaaaojoi_hfw422.png-wh_50 "/>

This article is from a "a little" blog, make sure to keep this source http://pengai.blog.51cto.com/6326789/1944652

Python's shades of Copy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.