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