Differences between revisions 2 and 3
Revision 2 as of 2023-03-01 15:24:52
Size: 1109
Comment:
Revision 3 as of 2023-04-11 15:45:05
Size: 1096
Comment:
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
== Usage ==



=== Copy ===

Make a shallow copy of an object with `copy()`.



=== DeepCopy ===

Make a '''deep copy''' of an object with `deepcopy()`.
== Concept ==
Line 32: Line 20:
The `deepcopy()` solution involves: Creating a deep copy then involves:
Line 36: Line 24:

----



== Usage ==

=== Copy ===

Make a shallow copy of an object.



=== DeepCopy ===

Make a deep copy of an object.

Python Copy

copy is a module for deep- and shallow-copying operations.


Concept

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:

  1. Recursive objects may contain a reference to themselves, and causing a recursion loop
  2. Some data should be copied once and shared among the contents

Creating a deep copy then involves:

  1. Allowing objects to override the operation to avoid loops; see __deepcopy__ for details

  2. Keeping a memo dictionary for data already copied, so that it can be shared among the contents


Usage

Copy

Make a shallow copy of an object.

DeepCopy

Make a deep copy of an object.


See also

Python copy module documentation


CategoryRicottone

Python/Copy (last edited 2023-10-10 20:43:25 by DominicRicottone)