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