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