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