home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / c / copy < prev    next >
Encoding:
Text File  |  1996-11-14  |  2.3 KB  |  63 lines

  1. <TITLE>copy -- Python library reference</TITLE>
  2. Next: <A HREF="../m/marshal" TYPE="Next">marshal</A>  
  3. Prev: <A HREF="../s/shelve" TYPE="Prev">shelve</A>  
  4. Up: <A HREF="../p/python_services" TYPE="Up">Python Services</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H1>3.6. Standard Module <CODE>copy</CODE></H1>
  7. This module provides generic (shallow and deep) copying operations.
  8. <P>
  9. Interface summary:
  10. <P>
  11. <UL COMPACT><CODE>import copy<P>
  12. <P>
  13. x = copy.copy(y)        # make a shallow copy of y<P>
  14. x = copy.deepcopy(y)    # make a deep copy of y<P>
  15. </CODE></UL>
  16. For module specific errors, <CODE>copy.error</CODE> is raised.
  17. <P>
  18. The difference between shallow and deep copying is only relevant for
  19. compound objects (objects that contain other objects, like lists or
  20. class instances):
  21. <P>
  22. <UL>
  23. <LI>•  A <I>shallow copy</I> constructs a new compound object and then (to the
  24. extent possible) inserts <I>references</I> into it to the objects found
  25. in the original.
  26. <P>
  27. <LI>•  A <I>deep copy</I> constructs a new compound object and then,
  28. recursively, inserts <I>copies</I> into it of the objects found in the
  29. original.
  30. <P>
  31. </UL>
  32. Two problems often exist with deep copy operations that don't exist
  33. with shallow copy operations:
  34. <P>
  35. <UL>
  36. <LI>•  Recursive objects (compound objects that, directly or indirectly,
  37. contain a reference to themselves) may cause a recursive loop.
  38. <P>
  39. <LI>•  Because deep copy copies <I>everything</I> it may copy too much, e.g.
  40. administrative data structures that should be shared even between
  41. copies.
  42. <P>
  43. </UL>
  44. Python's <CODE>deepcopy()</CODE> operation avoids these problems by:
  45. <P>
  46. <UL>
  47. <LI>•  keeping a table of objects already copied during the current
  48. copying pass; and
  49. <P>
  50. <LI>•  letting user-defined classes override the copying operation or the
  51. set of components copied.
  52. <P>
  53. </UL>
  54. This version does not copy types like module, class, function, method,
  55. nor stack trace, stack frame, nor file, socket, window, nor array, nor
  56. any similar types.
  57. <P>
  58. Classes can use the same interfaces to control copying that they use
  59. to control pickling: they can define methods called
  60. <CODE>__getinitargs__()</CODE>, <CODE>__getstate__()</CODE> and
  61. <CODE>__setstate__()</CODE>.  See the description of module <CODE>pickle</CODE>
  62. for information on these methods.
  63.