A brief analysis of Python's shades of Copy

Source: Internet
Author: User

Import Copy
#浅拷贝
person = ["Name", ["Saving", 1000]]
P1 = copy.copy (person)
P2 = person.copy () #两种都是浅拷贝
Print (PERSON,P1,P2)
# # #结果为 [' name ', [' saving ', [+]] [' Name ', [' saving ', ' +]] [' Name ', [' saving ', 1000]]
Person[0] = "Clark"
Print (PERSON,P1,P2)
# # #结果为 [' Clark ', [' saving ', +]] [' Name ', [' saving ', ' + '] [' name ', [' saving ', ' + ']] single character unchanged
PERSON[1][1] = 2000
Print (PERSON,P1,P2) #记住这两种方式的最后结果
# # # #结果为 [' Clark ', [' saving ', ' +]] [' Name ', [' saving ', ' +]] [' Name ', [' Saving ', ']] ' list value changed, corresponding to the list value in the light copy corresponding change

P1[0] = "Xu Hongbiao"
Print (PERSON,P1,P2)
# #结果为 [' Clark ', [' Saving ', 2000]] [' Xu Hongbiao ', [' Saving ', ' [']] [' Name ', [' Saving ', 2000]]
P1[1][1] = 3000
Print (PERSON,P1,P2)
# #结果为 [' Clark ', [' Saving ', 3000]] [' Xu Hongbiao ', [' saving ', ' +]] [' Name ', [' saving ', ' +]]   conclusion Ibid .

#深拷贝
Info = ["Name", ["Love", 20]]
Info1 = copy.deepcopy (info)
Info2 = copy.deepcopy (info)
Print (Info,info1,info2)
Info[0] = "Clark"
Print (Info,info1,info2)
INFO[1][1] = 30
Print (Info,info1,info2)
INFO1[1][1] = 40
Print (Info,info1,info2) #深拷贝, each independent so no matter how you modify any one, the other June unchanged






A brief analysis of 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.