Home >Backend Development >Python Tutorial >Object copy in Python

Object copy in Python

高洛峰
高洛峰Original
2016-10-19 13:39:051434browse

Do you want to copy an object? Because in Python, whether you pass an object as a parameter or as a function return value, it is passed by reference.

What is pass by reference? Let’s look at a C++ function that exchanges two numbers:

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

This example is an example of pass by reference! The purpose is to explain the concept: passing by reference means that what you pass is a reference to the object, and modifications to this reference will also cause changes to the original object. Friends who have studied C/C++ all know that when exchanging two numbers, if you implement a swap function yourself, you need to pass its reference or pointer.

Python uses reference passing directly, how convenient it is, what else do you want to complain about? Have you ever thought about the situation where I don’t want to change the original object? If so, then look here!

Suppose I now have a list called l1, and I now need a copy of l1. If I directly use a method such as l2 = l1, and then I make a series of modifications to l2, it will be equivalent to me directly modifying l1. Making changes is not what I want! Such as:

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 passing, which means that l1 and l2 belong to the same list object, so how can we get a different object? Isn’t this so easy? Sprinkle it in slices, for example:

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

Yes, the goal is achieved. By the way, are you sure this will work? Let’s look at a more complicated situation:

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]

Ah, something seems to be wrong, this is not what we need! How to do it? Okay, let’s get to today’s topic, the copy module in Python!

import copy

If you want to copy a container object and all the elements in it (including the child elements of the element), use copy.deepcopy. This method will consume some time and space. However, if you need to copy it completely, This is the only way. The slicing method we mentioned above is equivalent to the copy function in the copy module.

The above copy operation has become so easy:

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn