Python object copy example python reference Transfer

Source: Internet
Author: User
Do you want to copy an object? In Python, no matter whether you pass an object as a parameter or return a function value, it is a reference transfer. Let's look at a C ++ function that exchanges two numbers:

The Code is as follows:


Void swap (int & a, int & B)
{
Int temp;
Temp =;
A = B;
B = temp;
}

This example is an example of reference transfer! The purpose is to explain the concept: the reference transfer means that you pass the object reference, and the modification to this reference will also lead to changes to the original object. All those who have learned C/C ++ know that when switching two numbers, if you implement a swap function, you need to pass its reference or pointer.

How convenient it is to directly use the reference Transfer Function in Python. What do you need to talk about? Have you ever wondered if I don't want to change the original object? If so, let's take a look!

Assume that I now have a list called l1, and now I need a copy of l1. If I directly use methods such as l2 = l1, then I make a series of modifications to l2, it is equivalent to directly modifying l1. This is not what I want! For example:

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 caused by Python reference transfer. That is to say, l1 and l2 belong to the same list object. How can we get a different object? This is not so easy. Use slice to scatter, for example:

The Code is as follows:


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

Yes, the goal is achieved. Don't introduce it. Are you sure you want to do this? Let's look at a more complex 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 seems that something went wrong. This is not what we need! What should we do? Now, let's go to today's topic: copy module in Python!

The Code is as follows:


Import copy


If you want to copy a container object and all its elements (including its child elements), use copy. deepcopy, this method consumes some time and space. However, if you need to completely copy, this is the only method. The slicing method we mentioned above is equivalent to the copy function in the copy module.

The copy operation 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]

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.