Deep understanding of the copy module in python (light replication and deep replication)

Source: Internet
Author: User
This article mainly provides you with a deep understanding of the copy module (shortest copy and deep copy) in python and has some reference value, if you are interested, refer to the introduction of the copy module in python.

The copy module includes the function of creating a complex object (including lists, tuples, dictionaries, and user-defined object instances.

########

Copy (x)

########

Create a new composite object and reference the Copy x member to create a copy of x. In a deeper sense,

It copies the object, but still uses references for the elements in the object.

This function is not frequently used for built-in types.

Instead, we use call methods such as list (x), dict (x), and set (x) to create a shortest copy of x.

Directly using the type name is obviously much faster than using copy. However, they achieve the same effect.

Another point is for unmodifiable objects (strings, numbers, tuples), because you don't have to worry about modifying them. No replication

What is the significance.

In addition, you can use the is operator to determine whether objects are copied.

A is B-> True a and B reference the same object, not copy

-> False: a and B copy objects to each other.

Example:

(1)

>>> A = [1, 2, 3]

>>> B = copy. copy ()

>>> B

[1, 2, 3]

>>> A. append (4)

>>>

[1, 2, 3, 4]

>>> B

[1, 2, 3]

>>> A is B

False

(2)

>>> A = [1, 2, 3]

>>> B =

>>> B

[1, 2, 3]

>>> A. append (4)

>>>

[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 = [1, 2, 3]

>>> B = list ()

>>> B

[1, 2, 3]

>>> A. append (4)

>>>

[1, 2, 3, 4]

>>> B

[1, 2, 3]

>>>

(4)

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

>>> B = copy. copy ()

>>> Print a, B

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

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

>>> B

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

>>>

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

>>> B. append ([1, 100,101])

>>> B

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

>>>

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

In the (3) example, we can see that the shortest copying objects B of a are different objects, so the changes to objects will not

Each other is affected, but the elements of these a and B objects are referenced in the same one. Therefore, if a or B modifies its object elements

Another value.

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

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

Deepcopy (x [, visit])

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

Create a new composite object and duplicate all the members of x to create a deep copy of x.

Visit is an optional dictionary designed to track accessed objects and detect and avoid repeated definitions.

The cycle in the data structure.

Although this is usually not required, the following methods are implemented: _ copy _ (self) and _ deepcopy _ (self, visit ),

Class to implement custom replication methods. The two methods implement the shortest replication and deep replication operations respectively.

The _ deepcopy _ () method must use the dictionary visit to track the objects encountered in the replication process. For

_ Deepcopy _ () method, except for passing visit to other deepcopy () methods included in the implementation (if any,

There is no need to perform other operations.

If the class implements the methods _ getstate _ () and _ setstate _ () used by the pickle module, the copy module uses

These methods are used to create a copy.

But through implementation methods _ copy _ (self) and _ deepcopy _ (self, visit ),

Class to implement custom replication methods. The two methods implement the shortest replication and deep replication operations respectively.

The _ deepcopy _ () method must use the dictionary visit to track the objects encountered in the replication process. For

_ Deepcopy _ () method, except for passing visit to other deepcopy () methods included in the implementation (if any,

There is no need to perform other operations.

If the class implements the methods _ getstate _ () and _ setstate _ () used by the pickle module, the copy module uses

These methods are used to create copies ., But through implementation methods _ copy _ (self) and _ deepcopy _ (self, visit ),

Class to implement custom replication methods. The two methods implement the shortest replication and deep replication operations respectively.

The _ deepcopy _ () method must use the dictionary visit to track the objects encountered in the replication process. For

_ Deepcopy _ () method, except for passing visit to other deepcopy () methods included in the implementation (if any,

There is no need to perform other operations.

If the class implements the methods _ getstate _ () and _ setstate _ () used by the pickle module, the copy module uses

These methods are used to create a copy.

Eg:

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

>>> Import copy

>>> B = copy. deepcopy ()

>>> B

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

>>> C = copy. copy ()

>>> C

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

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

>>>

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

>>> B

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

>>> C

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

Note that:

(1) the copy module is used for simple types such as integers and strings, but this is rarely required.

(2) these replication functions cannot work with modules, class objects, functions, methods, backtracking, stack frames, files, sockets, and other similar types at the same time.

If the object cannot be copied, a copy. error exception is thrown.

The above is a detailed description of the copy module (shallow copy and deep copy) in python. For more information, see other related articles in the first PHP community!

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.