Python depth copy

Source: Internet
Author: User

The structure in which Python stores data in memory

1 Consider a question first why Python list can be appended, that is, the data will continue to expand?

When a list object is generated, Python creates an area in memory to hold the current value, and if the data in the Def memory is added sequentially, will it be stored continuously?

Of course not, because Python is the use of the link in C implementation of the Append way, Python created a list of the time must not know how much you need to store, so you can not open up a lot of memory space.

The linked list records the previous position and the next position, thus enabling additional functionality.

Here is a point, because Python is developed by C, and C is not the concept of a string, only a string array. Just like the picture above str= "ABC", ABC is the existence of three memory blocks respectively.

When Python modifies the str= "ADC", it opens up three new blocks of memory to store

Take a look at the following example:

Here is the value of list[0] changed to XXX, from the figure can be seen, 0 of the subscript used to point to ABC, 0 points to xxx, but ABC actually still in memory did not disappear.

Shallow copy

Python has an optimization mechanism for numbers and strings , where the memory address is the same regardless of the depth of the copy.

>>> n1=123
>>> n2=n1
>>> ID (N1)
7367920L
>>> ID (n2)
7367920L

>>> n1=123
>>> N2=copy.copy (N1)
>>> ID (N1)
7367920L
>>> ID (n2)
7367920L

>>> N2=copy.deepcopy (N1)
>>> ID (n2)
7367920L

As long as it is a number or a string then no matter how CP points to the same memory, actually the same thing open up 2 memory space will be wasted, because the values are the same.

What if I change the value of N1 to =456? So will it affect the value of N2? Of course not. N2 will still point to 123 of this memory address.

So what if the data type is a tuple, a list, or a dictionary?

Define a data type first

>>> n1={"K1": "abc", "K2": 123, "K3": ["Hello", "999"]}
>>> n2=n1

Shallow copy, N2 will only copy [K1,K2,K3] The first layer of memory ID.

Look at the effect of copying only the first layer:

2 ID, no mistake.

>> ID (n2[' k1 ')
4109480L
>> ID (n1[' k1 ')
4109480L

This changes if the direct CP is a string, and when A's value is changed it does not affect B, but the value of B here changes with a. This is the first layer of the CP problem. The reason is simple N2 just need to find K1 on the line, K1 pointer to who is not related to N2

>> n1["K1"]= "CCC"
>> ID (n1[' k1 ')
5415864L
>> ID (n2[' k1 ')
5415864L
>> n2["K1"]
CCC
>> n1["K1"]
CCC

Python depth 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.