home *** CD-ROM | disk | FTP | other *** search
- <TITLE>copy -- Python library reference</TITLE>
- Next: <A HREF="../m/marshal" TYPE="Next">marshal</A>
- Prev: <A HREF="../s/shelve" TYPE="Prev">shelve</A>
- Up: <A HREF="../p/python_services" TYPE="Up">Python Services</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H1>3.6. Standard Module <CODE>copy</CODE></H1>
- This module provides generic (shallow and deep) copying operations.
- <P>
- Interface summary:
- <P>
- <UL COMPACT><CODE>import copy<P>
- <P>
- x = copy.copy(y) # make a shallow copy of y<P>
- x = copy.deepcopy(y) # make a deep copy of y<P>
- </CODE></UL>
- For module specific errors, <CODE>copy.error</CODE> is raised.
- <P>
- The difference between shallow and deep copying is only relevant for
- compound objects (objects that contain other objects, like lists or
- class instances):
- <P>
- <UL>
- <LI>• A <I>shallow copy</I> constructs a new compound object and then (to the
- extent possible) inserts <I>references</I> into it to the objects found
- in the original.
- <P>
- <LI>• A <I>deep copy</I> constructs a new compound object and then,
- recursively, inserts <I>copies</I> into it of the objects found in the
- original.
- <P>
- </UL>
- Two problems often exist with deep copy operations that don't exist
- with shallow copy operations:
- <P>
- <UL>
- <LI>• Recursive objects (compound objects that, directly or indirectly,
- contain a reference to themselves) may cause a recursive loop.
- <P>
- <LI>• Because deep copy copies <I>everything</I> it may copy too much, e.g.
- administrative data structures that should be shared even between
- copies.
- <P>
- </UL>
- Python's <CODE>deepcopy()</CODE> operation avoids these problems by:
- <P>
- <UL>
- <LI>• keeping a table of objects already copied during the current
- copying pass; and
- <P>
- <LI>• letting user-defined classes override the copying operation or the
- set of components copied.
- <P>
- </UL>
- This version does not copy types like module, class, function, method,
- nor stack trace, stack frame, nor file, socket, window, nor array, nor
- any similar types.
- <P>
- Classes can use the same interfaces to control copying that they use
- to control pickling: they can define methods called
- <CODE>__getinitargs__()</CODE>, <CODE>__getstate__()</CODE> and
- <CODE>__setstate__()</CODE>. See the description of module <CODE>pickle</CODE>
- for information on these methods.
-