Python Learning copy Module

Source: Internet
Author: User

There are two methods in the Copy module, Copy.copy () and copy.deepcopy, which are used to implement both shallow and deep copies, respectively. The so-called shallow copy is a copy-only reference, the two variables generated after the copy is still the same object, that is, one change, and the other changes. For a deep copy, the data is copied, and the resulting two variables are different objects, and the changes between them do not affect each other. But one thing to note is that the two copy method of the copy module is about the elements of the object to be copied, that is, whether the copy () method or the Deepcopy () method, the object to be copied is a deep copy, generating two different objects, for example:


As you can see, both the copy () method and the Deepcopy () method are used to produce different objects, but if you use the copy () method, the object's element is a shallow copy if it is a mutable type of collection, class, or deepcopy () of the object. So if you change the A1, A2 and A3 will not have any effect, but if you change the first element of A1, the first element of A2 will change, but A3 will not have an effect, for example:



Precautions:

1, for the built-in type of shallow copy, generally not applicable to the copy () function, because all have their own factory function, and efficiency is much higher than the copy () function, such as: List (), Dict (), set (), etc.

2, for those non-modifiable objects (string, number, tuple, file,socket,module,method, etc.), because you do not have to worry about modifying them, copy does not have much meaning, so use "=", copy (), Deepcopy () Returns a reference to the same object without making any copies

3. Deepcopy () has an optional dictionary that is designed to track the objects being accessed, thus detecting and avoiding loops in a repeating defined data structure. For example, the list contains its own

4. Although not normally required, the class can implement a custom replication method by implementing the method __copy__ (self) and __deepcopy__, visit, which implements both shallow copy and deep copy operations. 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, there is no need to perform additional operations in addition to uploading visit to other deepcopy () methods contained in the implementation, if any.
If the class implements the methods used by the Pickle Module __getstate__ () and __setstate__ (), then the copy module uses these methods to create replicas, but through the implementation method __copy__ (self) and __deepcopy__ , visit), the class can implement a custom replication method, both of which implement shallow copy and deep copy operations, respectively.

Python Learning copy Module

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.