Introduction to Python memory usage (in-depth copy) and python

Source: Internet
Author: User

Introduction to Python memory usage (in-depth copy) and python

This article focuses on the usage of Python memory (deep copy). The details are as follows.

A shallow copy is a copy of the reference (only copying the parent object)

Deep copy is the copy of object Resources.

>>> a=[1,2,3,'a','b']>>> b=a>>> b[1, 2, 3, 'a', 'b']>>> a[1, 2, 3, 'a', 'b']>>> id(a)3021737547592>>> id(b)3021737547592>>> a.append('c')>>> a[1, 2, 3, 'a', 'b', 'c']>>> b[1, 2, 3, 'a', 'b', 'c']>>> b.append(4)>>> b[1, 2, 3, 'a', 'b', 'c', 4]>>> a[1, 2, 3, 'a', 'b', 'c', 4]

From the above operation, we can see that after assigning a to B, the addresses of a and B are the same. No matter which one changes, the other will change and remain the same.

>>> Import copy >>> a = [1, 2, 3, ['A', 'B ', 'C'] >>> B = a >>> c = copy. copy (a) >>> B [1, 2, 3, ['A', 'B', 'C'] >>> c [1, 2, 3, ['A', 'B', 'C'] >>>> id (a) 3021737548104 >>> id (B) 3021737548104 >>>> id (c) 3021737494536 # the address of the Child copy parent object is different >>>. append ('D') >>> a [1, 2, 3, ['A', 'B', 'C'], 'D']> B [1, 2, 3, ['A', 'B', 'C'], 'D']> c [1, 2, 3, ['A', 'B', 'C'] # the address of a and c is different. Therefore, a is changed, c does not change >>> id (a [0]) 1686357680 >>> id (c [0]) 1686357680 >>> id (a [3]) 3021737547528 >>> id (c [3]) 3021737547528 # the space occupied by the entire parent object is different, but the space occupied by the same inner data is the same >>> a [3]. append ('D') >>> a [1, 2, 3, ['A', 'B', 'C', 'D'], 'D ']> c [1, 2, 3, ['A',' B ', 'C ', 'D'] # because the data in the inner layer occupies the same space, a changes and c changes accordingly.

The above isShortest copy: The addresses of the entire parent object are different. The addresses of the inner layer data are the same. The operations on the inner layer data change together. When the operation object is a parent object, the copy object does not change.

>>> A [1, 2, 3, ['A', 'B', 'C', 'D'], 'D'] >>> d = copy. deepcopy (a) >>> d [1, 2, 3, ['A', 'B', 'C', 'D'], 'D'] >>>> id (a) 3021737548104 >>>> id (d) 3021737547656 # the address of the deep copy parent object is different >>>. append ('E') >>> a [1, 2, 3, ['A', 'B', 'C', 'D'], 'D ', 'E'] >>> d [1, 2, 3, ['A', 'B', 'C', 'D'], 'D'] # the address of a and d is different, so a changes, d does not change >>> id (a [0]) 1686357680 >>> id (d [0]) 1686357680 >>> id (a [3]) 3021737547528 >>> id (d [3]) 3021737493256 # the address of the inner layer data is different >>> a [3]. append ('x') >>> a [1, 2, 3, ['A', 'B', 'C', 'D', 'x'], 'D', 'E'] >>> d [1, 2, 3, ['A', 'B', 'C', 'D'], 'D']

The above isDeep copy.

Differences:

The shortest copy address is the same as the inner data address of the original object;
Deep copy is completely independent from the original object.

Summary

The above is all about Python's memory usage (in-depth copy). I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.