"Python trail 13" Python's deep-shade copy

Source: Internet
Author: User
Tags shallow copy

Depth copy

One, number and string

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

123456789101112131415 importcopy# ######### 数字、字符串 #########n1 =123# n1 = "i am alex age 10"print(id(n1))# ## 赋值 ##n2 =n1print(id(n2))# ## 浅拷贝 ##n2 =copy.copy(n1)print(id(n2)) # ## 深拷贝 ##n3 =copy.deepcopy(n1)print(id(n3))

Second, other basic data types

For dictionaries, Ganso, and lists, the changes in the memory address of the assignment, the shallow copy, and the deep copy are different.

1. Assign Value

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

123 n1  =   { "K1" "WU" "K2" 123 "K3" : [ "Alex"   456 ]}     n2  =   n1

  

2. Shallow copy

Shallow copy , creating only the first layer of data in memory

12345 importcopy n1 ={"k1""wu""k2"123"k3": ["alex"456]} n3 =copy.copy(n1)

3. Deep copy

deep copy , recreate all the data in memory (excluding the last layer, i.e., Python's internal optimization of strings and numbers)

12345 importcopy n1 = {"k1""wu""k2"123"k3": ["alex"456]} n4 = copy.deepcopy(n1)

"Python trail 13" Python's deep-shade copy

Related Article

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.