Learn more about copy modules in Python (shallow copy and deep copy)

Source: Internet
Author: User
The main introduction is the Copy module in Python.

The copy module includes deep-shading functions that create composite objects, including lists, tuples, dictionaries, and instances of user-defined objects.

########

Copy (x)

########

Creates a new composite object and creates a shallow copy of x by referencing the members of the copy X. In a more profound sense,

It duplicates the object, but the reference is still used for the elements in the object.

For built-in types, this function is not used frequently.

Instead, use calls such as List (x), Dict (x), set (x) to create a shallow copy of x, knowing that like this

It is obviously much faster to use the type name directly than with copy (). But they achieve the same effect.

Another thing is for those objects that cannot be modified (strings, numbers, tuples) because you don't have to worry about modifying them. Replication does not replicate and there is no

What a big meaning.

On the other hand, you determine whether the object is a copy, and you can use the IS operator to identify it.

A is B, True A and B refer to the same object, not copy

-A and B are copying objects to each other

For example, eg:

(1)

>>> a = [+ +]

>>> B = Copy.copy (a)

>>> b

[1, 2, 3]

>>> A.append (4)

>>> A

[1, 2, 3, 4]

>>> b

[1, 2, 3]

>>> A is B

False

(2)

>>> a = [+ +]

>>> B = A

>>> b

[1, 2, 3]

>>> A.append (4)

>>> A

[1, 2, 3, 4]

>>> b

[1, 2, 3, 4]

>>> B.append (6)

>>> A, b

([1, 2, 3, 4, 6], [1, 2, 3, 4, 6])

(3)

>>> a = [+ +]

>>> B = List (a)

>>> b

[1, 2, 3]

>>> A.append (4)

>>> A

[1, 2, 3, 4]

>>> b

[1, 2, 3]

>>>

(4)

>>> a = [[1], [' A '], [' a ']]

>>> B = Copy.copy (a)

>>> print A, b

[[1], [' A '], [' a ']] [[1], [' A '], [' a ']]

>>> b[1].append (' B ')

>>> b

[[1], [' A ', ' B '], [' a ']]

>>> A

[[1], [' A ', ' B '], [' a ']]

>>> b.append ([100,101])

>>> b

[[1], [' A ', ' B '], [' A '], [100, 101]]

>>> A

[[1], [' A ', ' B '], [' a ']]

In the (3) example, we can see A's shallow copy object B, which are different objects, so the change to the object is not

affect each other, but the elements of the A and B objects are the same as the reference, so a or B changes the elements of its object to affect

The value of the other one.

If you want to completely copy the values of an object and all the elements of an object, use only the following deepcopy () function.

#######################

Deepcopy (x[, visit])

#######################

Create a deep copy of x by creating a new composite object and duplicating all members of X repeatedly.

Visit is an optional dictionary that tracks the objects that are accessed to detect and avoid duplicate definitions

Loops in the data structure.

Although it is not normally required, by implementing the method __copy__ (self) and the __deepcopy__ (the. Visit),

class allows you to implement a custom replication method, both of which implement shallow and deep copy operations, respectively.

The __deepcopy__ () method must use the dictionary visit, which is used to track the previously encountered objects during the copy process. For

The __deepcopy__ () method, in addition to uploading visit to other deepcopy () methods contained in the implementation, if any,

There is no need to perform other operations.

If the class implements the methods used by the Pickle Module __getstate__ () and __setstate__ (), then the copy module uses the

These methods to create replicas.

, but by implementing the method __copy__ (self) and the __deepcopy__ (A. Visit),

class allows you to implement a custom replication method, both of which implement shallow and deep copy operations, respectively.

The __deepcopy__ () method must use the dictionary visit, which is used to track the previously encountered objects during the copy process. For

The __deepcopy__ () method, in addition to uploading visit to other deepcopy () methods contained in the implementation, if any,

There is no need to perform other operations.

If the class implements the methods used by the Pickle Module __getstate__ () and __setstate__ (), then the copy module uses the

These methods to create replicas. , but by implementing the method __copy__ (self) and the __deepcopy__ (A. Visit),

class allows you to implement a custom replication method, both of which implement shallow and deep copy operations, respectively.

The __deepcopy__ () method must use the dictionary visit, which is used to track the previously encountered objects during the copy process. For

The __deepcopy__ () method, in addition to uploading visit to other deepcopy () methods contained in the implementation, if any,

There is no need to perform other operations.

If the class implements the methods used by the Pickle Module __getstate__ () and __setstate__ (), then the copy module uses the

These methods to create replicas.

eg

>>> a = [[1], [' A '], [' a ']]

>>> Import Copy

>>> B = Copy.deepcopy (a)

>>> b

[[1], [' A '], [' a ']]

>>> C = copy.copy (a)

>>> C

[[1], [' A '], [' a ']]

>>> a[1].append (' B ')

>>> A

[[1], [' A ', ' B '], [' a ']]

>>> b

[[1], [' A '], [' a ']]

>>> C

[[1], [' A ', ' B '], [' a ']]

It is important to note that:

(1) The Copy module is used for simple types such as integers and strings, but it is seldom necessary to do so.

(2) These copy functions cannot work concurrently with modules, class objects, functions, methods, backtracking, stack frames, files, sockets, and other similar types.

If the object cannot be copied, an Copy.error exception is thrown.

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.