Python depth copy with pointer memory

Source: Internet
Author: User

Python is a very good language, his advantage is that there is great flexibility at the same time also has the incomparable rigor, other languages prescribe a lot of grammar, tell you what the syntax is this, and Python, with very few rules, extended a lot of grammar, some syntax seems strange, Careful analysis, however, is so reasonable. Today I've been thinking about pointers and shades in python, and hopefully we'll be able to explain these headaches by changing the memory space.

First look at the first example:

A = 1

b = A

b = 2

print ' A =%s '%a, ' B =%s '%b

Result: A = 1 B = 2


When performing a = 1 operation, the memory allocates a space for an integer variable and assigns a value of 1, then points A to the space, and B = A When B is also pointed to the memory, where A and B are the same position. b = 2, which is supposed to assign a value of 2 to the memory that the current B points to, but Python does not. Because in Python, integers and other basic types, along with strings and tuples, are not modifiable. It means that once they are declared, they cannot be changed in memory. Then execution B = 2 o'clock occurs when the system allocates an integer memory and assigns a value to it, and then assigns the address of the memory to B, so that A and B point to different values.

Look at a very contradictory example:

a=[1,[2],3]

B=a

B[0]=0

Print a

Results: [0,[2],3]

This seems not quite right, and the above is a bit contradictory, in fact, it is not contradictory. Because lists can be modified, a and B point to a list, in fact the list is a set of pointers. The pointer can be modified. Tuples cannot be modified simply because the data in the tuple is the data on the memory itself, and the list element is a pointer. When using B to modify b[0], it is actually pointing to a 1 pointer b[0] and also a[0] pointing to a 0, and that 1 if there is no other pointer to him, he is recycled. This 0 and 1, although indexed, have different locations in memory. I really want to be able to print the contents of the pointer to verify my idea.

The memory situation is as follows:


After assignment:

Let's say copy () and Deepcopy ().

Deepcopy () Needless to say, this is strictly a deep copy. Two pointers have nothing to do with it, and no one will affect who, because they are independent in memory. and copy is not, he is copy () different from the direct assignment, but it is not the same as deepcopy (). As an example:

Import Copy

a=[1,[2],3]

B=copy.copy (a)

B[0]=0

B[1].append (2)

Print a

The result is: [1,[2,2],3]

The result of the folding seems unexpected, in fact, it is very reasonable. Copy () is a shallow copy, B is actually a shadow of a, the system also allocates its own space for B, meaning that A and B point to the memory is not the same, but B only copy the pointer in a, meaning that each item of B and each item in a points to the same memory. When B[0] is assigned a value of 0 o'clock, b[0] changes from point 1 to 0, and A[0] to 1, so the first item of a is still 1. While B[1].append (2), b[1] and a[1] point to the same list, this list is [2]. Instead of modifying the pointer, the statement adds an element to [2]. A[1] and b[1] still point to this list. Thus a[1] becomes [2,2]. The picture should look like this:

is the memory condition after copy (). After the operation, the memory is as follows:

The above v is my understanding of Python memory management and pointers. If there is any mistake, please correct me.


Python depth copy with pointer memory

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.