/Doc/library/copy.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 87 lines · 58 code · 29 blank · 0 comment · 0 complexity · c622595fd39f0218d5e0e61f0cea39d9 MD5 · raw file

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