Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | This module provides generic (shallow and deep) copying operations. |
| 8 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
Georg Brandl | a59750f | 2009-09-09 16:51:05 +0000 | [diff] [blame] | 10 | Interface summary: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
Georg Brandl | a59750f | 2009-09-09 16:51:05 +0000 | [diff] [blame] | 12 | .. function:: copy(x) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
Georg Brandl | a59750f | 2009-09-09 16:51:05 +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 | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 | |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +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 | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | .. index:: module: pickle |
| 63 | |
| 64 | Classes can use the same interfaces to control copying that they use to control |
| 65 | pickling. See the description of module :mod:`pickle` for information on these |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 66 | methods. The :mod:`copy` module does not use the :mod:`copyreg` registration |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | module. |
| 68 | |
| 69 | .. index:: |
| 70 | single: __copy__() (copy protocol) |
| 71 | single: __deepcopy__() (copy protocol) |
| 72 | |
| 73 | In order for a class to define its own copy implementation, it can define |
| 74 | special methods :meth:`__copy__` and :meth:`__deepcopy__`. The former is called |
| 75 | to implement the shallow copy operation; no additional arguments are passed. |
| 76 | The latter is called to implement the deep copy operation; it is passed one |
| 77 | argument, the memo dictionary. If the :meth:`__deepcopy__` implementation needs |
| 78 | to make a deep copy of a component, it should call the :func:`deepcopy` function |
| 79 | with the component as first argument and the memo dictionary as second argument. |
| 80 | |
| 81 | |
| 82 | .. seealso:: |
| 83 | |
| 84 | Module :mod:`pickle` |
| 85 | Discussion of the special methods used to support object state retrieval and |
| 86 | restoration. |
| 87 | |