A deep copy problem for Python

Source: Internet
Author: User

I had a little program a few days ago and I ran into Python's deep-and-dark copy problem.

Feel their foundation is not solid enough to turn over the tutorial, really is very basic knowledge ... (Come on, you big guy. 2333)

Here is a brief talk about:

First there is a dictionary a

1 a = {'a': 1,'B': 2,'C' : 3,'D':4}

Then we assign it to another empty dictionary B

1 b = {}2 b = A

When we output B, we can see that A and B are intuitively seen as being equal.

In common sense, we would think that A and B are now two dictionaries of equal value, and if we do one of these, the other one should not be affected, so let's try:

We're trying to remove a key value pair from B.

1 b.pop ('A')

Output b

Naturally B One of the key-value pairs was removed

And then we tried to output a, something weird happened.

We just delete the key-value pairs for B, and how does the value of a change?

The reason is that Python's assignment principle

In fact, what we call a assignment to B is not a value assigned to B, but an address of the value stored by a.

So when we output B, the output is the same value as a.

Therefore, what we do with B will affect the in-memory {' A ': 1, ' B ': 2, ' C ': 3, ' D ': 4}

When we delete a key value pair of B, it is actually the deletion of the key value pairs {' A ': 1, ' B ': 2, ' C ': 3, ' D ': 4}

At this point A and B point to the new dictionary {' B ': 2, ' C ': 3, ' D ': 4}

So when we output, we find the value change.

So, how do you really copy the values?

This involves Python's deep-and-dark copy problem.

Use the Python copy () and Deepcopy () methods to achieve shallow and deep copies

What are shallow copies and deep copies?

A shallow copy copies only the parent object in the object, but does not copy it along with its child objects

A deep copy is a copy of all objects in the object including their child objects

As an example:

(Because the copy () method and the Deepcopy () method need to call the copy module, we first refer to it earlier)

Import Copy

Define Dictionary A

1 a = {'a': 1,'B': 2,'C' : 3,'D': [All-in-all]}

As a control group, we create B, also assign a to it (in fact, you should know why)

b = A

Now let's start with a shallow copy.

Create C to receive a shallow copy of a

1 C = copy.copy (a)

The next deep copy

1 d = copy.deepcopy (a)

After that, we make a series of changes to a to observe the change in the value of B,c,d

First, we delete a key value pair for a.

1 a.pop ('a')

Then we change the sub-object of the a middle key to D, and we add a sub-object in D

a['D'].append (4)

Now, we're going to output a,b,c,d, and we'll see how they differ.

The first is a, the obvious result

Second is B, because the reason assigned to the reference is also changed, same as the value of a

And then output C, found the clue

And look at D.

We found that both the parent object and its child objects in A and B were changed

C Although the parent object is not affected, but the element value (list) of which the key value is D has changed, so we find that the shallow copy will only copy the parent object in the object, and its child objects will still receive the same effect when the source data receives the change

And then turn around and look at D, exactly the same as the A before we changed it, so deep copy implements the object copy very completely, so we find that the deep copy can copy the entire contents of the object including its sub-objects.

To be more intuitive, refer to the following diagram:

As a result, copies can be considered as appropriate when copying data. Of course, there are also variables of variable and immutable types of data types, and there is no much discussion here.

This part of the article refers to the Alex Big guy's blog, the depth of the Python copy of a very detailed discussion, it is recommended that everyone to learn

Portal Https://m.2cto.com/net/201606/515654.html

A deep copy problem for Python

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.