|
⇤ ← Revision 1 as of 2023-01-08 05:25:37
Size: 995
Comment:
|
Size: 1109
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 37: | Line 37: |
| ---- == See also == [[https://docs.python.org/3/library/copy.html|Python copy module documentation]] |
Python Copy
copy is a module for deep- and shallow-copying operations.
Contents
Usage
Copy
Make a shallow copy of an object with copy().
DeepCopy
Make a deep copy of an object with deepcopy().
A deep copy differs from a shallow copy only for containers. A shallow copy contains references to the same objects that the original contains. A deep copy contains copies of the original's contents.
This poses two challenges:
- Recursive objects may contain a reference to themselves, and causing a recursion loop
- Some data should be copied once and shared among the contents
The deepcopy() solution involves:
Allowing objects to override the operation to avoid loops; see __deepcopy__ for details
Keeping a memo dictionary for data already copied, so that it can be shared among the contents
See also
Python copy module documentation
