blob: 5ae79aa91d1c7bd99bcc7243cc553922faa60037 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`copy` --- Shallow and deep copy operations
2================================================
3
4.. module:: copy
5 :synopsis: Shallow and deep copy operations.
6
Georg Brandl116aa622007-08-15 14:28:22 +00007This module provides generic (shallow and deep) copying operations.
8
Georg Brandl116aa622007-08-15 14:28:22 +00009
Georg Brandla59750f2009-09-09 16:51:05 +000010Interface summary:
Georg Brandl116aa622007-08-15 14:28:22 +000011
Georg Brandla59750f2009-09-09 16:51:05 +000012.. function:: copy(x)
Georg Brandl116aa622007-08-15 14:28:22 +000013
Georg Brandla59750f2009-09-09 16:51:05 +000014 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 Brandl116aa622007-08-15 14:28:22 +000026
Georg Brandl116aa622007-08-15 14:28:22 +000027The difference between shallow and deep copying is only relevant for compound
28objects (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
36Two problems often exist with deep copy operations that don't exist with shallow
37copy 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
45The :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
53This module does not copy types like module, method, stack trace, stack frame,
54file, socket, window, array, or any similar types. It does "copy" functions and
55classes (shallow and deeply), by returning the original object unchanged; this
56is compatible with the way these are treated by the :mod:`pickle` module.
57
Thomas Wouters89d996e2007-09-08 17:39:28 +000058Shallow copies of dictionaries can be made using :meth:`dict.copy`, and
59of lists by assigning a slice of the entire list, for example,
60``copied_list = original_list[:]``.
61
Georg Brandl116aa622007-08-15 14:28:22 +000062.. index:: module: pickle
63
64Classes can use the same interfaces to control copying that they use to control
65pickling. See the description of module :mod:`pickle` for information on these
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000066methods. The :mod:`copy` module does not use the :mod:`copyreg` registration
Georg Brandl116aa622007-08-15 14:28:22 +000067module.
68
69.. index::
70 single: __copy__() (copy protocol)
71 single: __deepcopy__() (copy protocol)
72
73In order for a class to define its own copy implementation, it can define
74special methods :meth:`__copy__` and :meth:`__deepcopy__`. The former is called
75to implement the shallow copy operation; no additional arguments are passed.
76The latter is called to implement the deep copy operation; it is passed one
77argument, the memo dictionary. If the :meth:`__deepcopy__` implementation needs
78to make a deep copy of a component, it should call the :func:`deepcopy` function
79with 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