Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame^] | 1 | \section{Standard Module \sectcode{copy}} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 2 | \stmodindex{copy} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame^] | 3 | \renewcommand{\indexsubitem}{(copy function)} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 4 | \ttindex{copy} |
| 5 | \ttindex{deepcopy} |
| 6 | |
| 7 | This module provides generic (shallow and deep) copying operations. |
| 8 | |
| 9 | Interface summary: |
| 10 | |
| 11 | \begin{verbatim} |
| 12 | import copy |
| 13 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 14 | x = copy.copy(y) # make a shallow copy of y |
| 15 | x = copy.deepcopy(y) # make a deep copy of y |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 16 | \end{verbatim} |
| 17 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame^] | 18 | For module specific errors, \code{copy.error} is raised. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 19 | |
| 20 | The difference between shallow and deep copying is only relevant for |
| 21 | compound objects (objects that contain other objects, like lists or |
| 22 | class instances): |
| 23 | |
| 24 | \begin{itemize} |
| 25 | |
| 26 | \item |
| 27 | A {\em shallow copy} constructs a new compound object and then (to the |
| 28 | extent possible) inserts {\em references} into it to the objects found |
| 29 | in the original. |
| 30 | |
| 31 | \item |
| 32 | A {\em deep copy} constructs a new compound object and then, |
| 33 | recursively, inserts {\em copies} into it of the objects found in the |
| 34 | original. |
| 35 | |
| 36 | \end{itemize} |
| 37 | |
| 38 | Two problems often exist with deep copy operations that don't exist |
| 39 | with shallow copy operations: |
| 40 | |
| 41 | \begin{itemize} |
| 42 | |
| 43 | \item |
| 44 | Recursive objects (compound objects that, directly or indirectly, |
| 45 | contain a reference to themselves) may cause a recursive loop. |
| 46 | |
| 47 | \item |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 48 | Because deep copy copies {\em everything} it may copy too much, e.g.\ |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 49 | administrative data structures that should be shared even between |
| 50 | copies. |
| 51 | |
| 52 | \end{itemize} |
| 53 | |
| 54 | Python's \code{deepcopy()} operation avoids these problems by: |
| 55 | |
| 56 | \begin{itemize} |
| 57 | |
| 58 | \item |
| 59 | keeping a table of objects already copied during the current |
| 60 | copying pass; and |
| 61 | |
| 62 | \item |
| 63 | letting user-defined classes override the copying operation or the |
| 64 | set of components copied. |
| 65 | |
| 66 | \end{itemize} |
| 67 | |
| 68 | This version does not copy types like module, class, function, method, |
| 69 | nor stack trace, stack frame, nor file, socket, window, nor array, nor |
| 70 | any similar types. |
| 71 | |
| 72 | Classes can use the same interfaces to control copying that they use |
| 73 | to control pickling: they can define methods called |
| 74 | \code{__getinitargs__()}, \code{__getstate__()} and |
| 75 | \code{__setstate__()}. See the description of module \code{pickle} |
| 76 | for information on these methods. |
| 77 | \stmodindex{pickle} |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame^] | 78 | \renewcommand{\indexsubitem}{(copy protocol)} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 79 | \ttindex{__getinitargs__} |
| 80 | \ttindex{__getstate__} |
| 81 | \ttindex{__setstate__} |