Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{copy} --- |
| 2 | Shallow and deep copy operations.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{copy} |
| 4 | |
| 5 | \modulesynopsis{Shallow and deep copy operations.} |
| 6 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 7 | \setindexsubitem{(copy function)} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 8 | \ttindex{copy} |
| 9 | \ttindex{deepcopy} |
| 10 | |
| 11 | This module provides generic (shallow and deep) copying operations. |
| 12 | |
| 13 | Interface summary: |
| 14 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 15 | \begin{verbatim} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 16 | import copy |
| 17 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 18 | x = copy.copy(y) # make a shallow copy of y |
| 19 | x = copy.deepcopy(y) # make a deep copy of y |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 20 | \end{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 21 | % |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 22 | For module specific errors, \code{copy.error} is raised. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 23 | |
| 24 | The difference between shallow and deep copying is only relevant for |
| 25 | compound objects (objects that contain other objects, like lists or |
| 26 | class instances): |
| 27 | |
| 28 | \begin{itemize} |
| 29 | |
| 30 | \item |
Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 31 | A \emph{shallow copy} constructs a new compound object and then (to the |
| 32 | extent possible) inserts \emph{references} into it to the objects found |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 33 | in the original. |
| 34 | |
| 35 | \item |
Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 36 | A \emph{deep copy} constructs a new compound object and then, |
| 37 | recursively, inserts \emph{copies} into it of the objects found in the |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 38 | original. |
| 39 | |
| 40 | \end{itemize} |
| 41 | |
| 42 | Two problems often exist with deep copy operations that don't exist |
| 43 | with shallow copy operations: |
| 44 | |
| 45 | \begin{itemize} |
| 46 | |
| 47 | \item |
| 48 | Recursive objects (compound objects that, directly or indirectly, |
| 49 | contain a reference to themselves) may cause a recursive loop. |
| 50 | |
| 51 | \item |
Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 52 | Because deep copy copies \emph{everything} it may copy too much, e.g.\ |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 53 | administrative data structures that should be shared even between |
| 54 | copies. |
| 55 | |
| 56 | \end{itemize} |
| 57 | |
| 58 | Python's \code{deepcopy()} operation avoids these problems by: |
| 59 | |
| 60 | \begin{itemize} |
| 61 | |
| 62 | \item |
Guido van Rossum | cf51dac | 1998-06-30 16:54:33 +0000 | [diff] [blame] | 63 | keeping a ``memo'' dictionary of objects already copied during the current |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 64 | copying pass; and |
| 65 | |
| 66 | \item |
| 67 | letting user-defined classes override the copying operation or the |
| 68 | set of components copied. |
| 69 | |
| 70 | \end{itemize} |
| 71 | |
| 72 | This version does not copy types like module, class, function, method, |
| 73 | nor stack trace, stack frame, nor file, socket, window, nor array, nor |
| 74 | any similar types. |
| 75 | |
| 76 | Classes can use the same interfaces to control copying that they use |
| 77 | to control pickling: they can define methods called |
| 78 | \code{__getinitargs__()}, \code{__getstate__()} and |
| 79 | \code{__setstate__()}. See the description of module \code{pickle} |
| 80 | for information on these methods. |
Guido van Rossum | cf51dac | 1998-06-30 16:54:33 +0000 | [diff] [blame] | 81 | The copy module does not use the \module{copy_reg} registration |
| 82 | module. |
Fred Drake | 54820dc | 1997-12-15 21:56:05 +0000 | [diff] [blame] | 83 | \refstmodindex{pickle} |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 84 | \setindexsubitem{(copy protocol)} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 85 | \ttindex{__getinitargs__} |
| 86 | \ttindex{__getstate__} |
| 87 | \ttindex{__setstate__} |
Guido van Rossum | cf51dac | 1998-06-30 16:54:33 +0000 | [diff] [blame] | 88 | |
| 89 | In order for a class to define its own copy implementation, it can |
| 90 | define special methods \method{__copy__()}\ttindex{__copy__} and |
| 91 | \method{__deepcopy__()}\ttindex{__deepcopy__}. The former is called to |
| 92 | implement the shallow copy operation; no additional arguments are |
| 93 | passed. The latter is called to implement the deep copy operation; it |
| 94 | is passed one argument, the memo dictionary. If the |
| 95 | \method{__deepcopy__()} implementation needs to make a deep copy of a |
| 96 | component, it should call the \function{deepcopy()} function with the |
| 97 | component as first argument and the memo dictionary as second |
| 98 | argument. |