Example of an object copy in Python python reference pass _python

Source: Internet
Author: User
Tags shallow copy in python

What is reference passing, let's look at a C + + Exchange two-number function:

Copy Code code 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 explain the concept: the meaning of the reference is that you are passing the reference of the object, and the modification of the reference will lead to the change of the original object. Friends who have learned C + + have known that in exchange for 2 numbers, if you implement a swap function, you need to pass its reference or pointer.

Python direct use of reference delivery, how convenient ah, you have to spit what? Did you ever think 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 way like L2 = L1, and then I make a series of changes to L2, it will be equivalent to my L1 to make changes directly, which is not what I want! Such as:

Copy Code code 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 delivery, which means that L1 and L2 belong to the same list object, so how do you get a different object? This is not easy, use slices to sprinkle, for example:

Copy Code code as follows:

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

Yes, the goal is achieved, don't tell, are you sure this will work? Let's look at a more complicated situation:

Copy Code code 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, there seems to be a problem. Ha, this is not what we need! What do we do? OK, go to today's topic, the Copy module in Python!

Copy Code code as follows:

Import Copy

If you want to replicate a container object and all of its elements (including 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 the slices we mentioned above are equivalent to the copy function in the copy module.

The operation of the above copy becomes so easy:

Copy Code code 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:

Copy Code code as follows:

Copy (x)
Shallow copy operation on arbitrary Python objects.
The module ' s __doc__ string for more info.
Deepcopy (x, Memo=none, _nil=[])
Deep copy operation on arbitrary Python objects.
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.