Deep, shallow copy

Source: Internet
Author: User

1, first for numbers and strings, the depth of the copy is meaningless, both point to the same memory address.

1 a = 1232print(ID (a))34 b = a5Print  (ID (b))678 4958497449 495849744

2, for dictionaries, Ganso, lists, the assignment, shallow copy and deep copy, its memory address changes are different.

Shallow copy, memory address unchanged, two times assignment all point to the same memory address.

Import= {"K1""v1" "K2  "K3": ["v2", 222]}Print = Copy.copy  (a)print(ID (b)) 1027860010278600 

Deep copy, all data is recreated in memory (excluding the last layer), which in fact excludes the last layer of strings and numbers.

1 ImportCopy2 3A = {"K1":"v1","K2": 111,"K3": ["v2", 222]}4 Print(ID (a))5 6b =Copy.deepcopy (a)7 Print(ID (b))8 9 Ten10147528 One10598792

Deep, shallow 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.