Object copy in Python sample Python reference pass

Source: Internet
Author: User
For reference passing, let's look at a C + + Exchange two number function:

The code is as follows:


void swap (int &a, int &b)
{
int temp;
temp = A;
A = b;
b = temp;
}

This example is an example of a reference pass! The purpose is to illustrate the concept that the reference passing means that you are passing an object reference, and the modification of that reference will also result in changes to the original object. Friends who have learned C + + know that when swapping 2 numbers, if you implement a swap function, you need to pass its reference or pointer.

Python directly using the reference pass, how convenient ah, you still have to spit groove what? Did you ever wonder if I didn't want to change the original object? If there is, then look here!

Assuming I now have a list called L1, I now need a copy of L1, and if I use a method such as L2 = L1, then I will make a series of changes to L2, which would be equivalent to the changes I made directly to L1, which is not what I want! Such as:

The code is as follows:


L1 = [1, 2]
L2 = L1
L2.append (3)
Print L1
Print L2
# L1 = [1, 2, 3], L2 = [1, 2, 3]

This is the result of Python reference passing, which means that L1 and L2 belong to the same list object, so how do you get a different object? This is not so easy, and it is sprinkled with slices, for example:

The code is as follows:


L1 = [1, 2]
L2 = l1[:]
L2.append (3)
# L1 = [1, 2], L2 = [1, 2, 3]

Yes, that's the goal, don't you think? Let's look at a more complicated situation:

The code is as follows:


L1 = [[1, 2], 3]
L2 = l1[:]
L2.append (4)
# L1 = [[1, 2], 3], L2 = [[1, 2], 3, 4]
L2[0].append (5)
# L1 = [[1, 2, 5], 3], L2 = [[1, 2, 5], 3, 4]

Aha, it looks like something's wrong. Ah, this is not what we need! What do we do? OK, get to the point of today, copy module in Python!

The code is as follows:


Import Copy


If you want to copy a container object, and all of its elements (containing the element's child elements), using Copy.deepcopy, this method consumes some time and space, but if you need to replicate completely, this is the only way. The way we refer to the slices above is equivalent to the copy function in the copy module.

The copy above has become so easy:

The code is as follows:


L1 = [[1, 2], 3]
L2 = copy.copy (L1)
L3 = copy.deepcopy (L1)
L2.append (4)
L2[0].append (5)
L3[0].append (6)
# L1 = [[1, 2, 5], 3], L2 = [[1, 2, 5], 3, 4], L3 = [[1, 2, 6], 3]

Related instructions:

The code is as follows:


Copy (x)
Shallow copy operation on arbitrary Python objects.
See the module ' s __doc__ string for more info.
Deepcopy (x, Memo=none, _nil=[])
Deep copy operation on arbitrary Python objects.
See the module ' s __doc__ string for more info.

  • 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.