~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Doc/library/copy.rst

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

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