Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :mod:`copy` --- Shallow and deep copy operations |
| 2 | ================================================ |
| 3 | |
| 4 | .. module:: copy |
| 5 | :synopsis: Shallow and deep copy operations. |
| 6 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 7 | This module provides generic (shallow and deep) copying operations. |
| 8 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 9 | |
Georg Brandl | ffdde9e | 2009-09-09 16:49:13 +0000 | [diff] [blame] | 10 | Interface summary: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 11 | |
Georg Brandl | ffdde9e | 2009-09-09 16:49:13 +0000 | [diff] [blame] | 12 | .. function:: copy(x) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 13 | |
Georg Brandl | ffdde9e | 2009-09-09 16:49:13 +0000 | [diff] [blame] | 14 | Return a shallow copy of *x*. |
| 15 | |
| 16 | |
| 17 | .. function:: deepcopy(x) |
| 18 | |
| 19 | Return a deep copy of *x*. |
| 20 | |
| 21 | |
| 22 | .. exception:: error |
| 23 | |
| 24 | Raised for module specific errors. |
| 25 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 26 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 27 | The difference between shallow and deep copying is only relevant for compound |
| 28 | objects (objects that contain other objects, like lists or class instances): |
| 29 | |
| 30 | * A *shallow copy* constructs a new compound object and then (to the extent |
| 31 | possible) inserts *references* into it to the objects found in the original. |
| 32 | |
| 33 | * A *deep copy* constructs a new compound object and then, recursively, inserts |
| 34 | *copies* into it of the objects found in the original. |
| 35 | |
| 36 | Two problems often exist with deep copy operations that don't exist with shallow |
| 37 | copy operations: |
| 38 | |
| 39 | * Recursive objects (compound objects that, directly or indirectly, contain a |
| 40 | reference to themselves) may cause a recursive loop. |
| 41 | |
| 42 | * Because deep copy copies *everything* it may copy too much, e.g., |
| 43 | administrative data structures that should be shared even between copies. |
| 44 | |
| 45 | The :func:`deepcopy` function avoids these problems by: |
| 46 | |
| 47 | * keeping a "memo" dictionary of objects already copied during the current |
| 48 | copying pass; and |
| 49 | |
| 50 | * letting user-defined classes override the copying operation or the set of |
| 51 | components copied. |
| 52 | |
| 53 | This module does not copy types like module, method, stack trace, stack frame, |
| 54 | file, socket, window, array, or any similar types. It does "copy" functions and |
| 55 | classes (shallow and deeply), by returning the original object unchanged; this |
| 56 | is compatible with the way these are treated by the :mod:`pickle` module. |
| 57 | |
Mark Summerfield | fcb444a | 2007-09-04 08:16:15 +0000 | [diff] [blame] | 58 | Shallow copies of dictionaries can be made using :meth:`dict.copy`, and |
| 59 | of lists by assigning a slice of the entire list, for example, |
| 60 | ``copied_list = original_list[:]``. |
| 61 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 62 | .. versionchanged:: 2.5 |
| 63 | Added copying functions. |
| 64 | |
| 65 | .. index:: module: pickle |
| 66 | |
| 67 | Classes can use the same interfaces to control copying that they use to control |
| 68 | pickling. See the description of module :mod:`pickle` for information on these |
Georg Brandl | dffbf5f | 2008-05-20 07:49:57 +0000 | [diff] [blame] | 69 | methods. The :mod:`copy` module does not use the :mod:`copy_reg` registration |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 70 | module. |
| 71 | |
| 72 | .. index:: |
| 73 | single: __copy__() (copy protocol) |
| 74 | single: __deepcopy__() (copy protocol) |
| 75 | |
| 76 | In order for a class to define its own copy implementation, it can define |
| 77 | special methods :meth:`__copy__` and :meth:`__deepcopy__`. The former is called |
| 78 | to implement the shallow copy operation; no additional arguments are passed. |
| 79 | The latter is called to implement the deep copy operation; it is passed one |
| 80 | argument, the memo dictionary. If the :meth:`__deepcopy__` implementation needs |
| 81 | to make a deep copy of a component, it should call the :func:`deepcopy` function |
| 82 | with the component as first argument and the memo dictionary as second argument. |
| 83 | |
| 84 | |
| 85 | .. seealso:: |
| 86 | |
| 87 | Module :mod:`pickle` |
| 88 | Discussion of the special methods used to support object state retrieval and |
| 89 | restoration. |
| 90 | |