Commit 3c9f05a5 authored by Fred Drake's avatar Fred Drake

Markup improvements to help with conversions.

parent ed980be7
...@@ -4,9 +4,9 @@ ...@@ -4,9 +4,9 @@
\modulesynopsis{Shallow and deep copy operations.} \modulesynopsis{Shallow and deep copy operations.}
\setindexsubitem{(copy function)} \withsubitem{(in module copy)}{%
\ttindex{copy} \ttindex{copy}%
\ttindex{deepcopy} \ttindex{deepcopy}}
This module provides generic (shallow and deep) copying operations. This module provides generic (shallow and deep) copying operations.
...@@ -19,7 +19,7 @@ x = copy.copy(y) # make a shallow copy of y ...@@ -19,7 +19,7 @@ x = copy.copy(y) # make a shallow copy of y
x = copy.deepcopy(y) # make a deep copy of y x = copy.deepcopy(y) # make a deep copy of y
\end{verbatim} \end{verbatim}
% %
For module specific errors, \code{copy.error} is raised. For module specific errors, \exception{copy.error} is raised.
The difference between shallow and deep copying is only relevant for The difference between shallow and deep copying is only relevant for
compound objects (objects that contain other objects, like lists or compound objects (objects that contain other objects, like lists or
...@@ -49,13 +49,13 @@ Recursive objects (compound objects that, directly or indirectly, ...@@ -49,13 +49,13 @@ Recursive objects (compound objects that, directly or indirectly,
contain a reference to themselves) may cause a recursive loop. contain a reference to themselves) may cause a recursive loop.
\item \item
Because deep copy copies \emph{everything} it may copy too much, e.g.\ Because deep copy copies \emph{everything} it may copy too much,
administrative data structures that should be shared even between e.g., administrative data structures that should be shared even
copies. between copies.
\end{itemize} \end{itemize}
Python's \code{deepcopy()} operation avoids these problems by: The \function{deepcopy()} function avoids these problems by:
\begin{itemize} \begin{itemize}
...@@ -75,24 +75,30 @@ any similar types. ...@@ -75,24 +75,30 @@ any similar types.
Classes can use the same interfaces to control copying that they use Classes can use the same interfaces to control copying that they use
to control pickling: they can define methods called to control pickling: they can define methods called
\code{__getinitargs__()}, \code{__getstate__()} and \method{__getinitargs__()}, \method{__getstate__()} and
\code{__setstate__()}. See the description of module \code{pickle} \method{__setstate__()}. See the description of module
for information on these methods. \module{pickle}\refstmodindex{pickle} for information on these
The copy module does not use the \module{copy_reg} registration methods. The \module{copy} module does not use the \module{copy_reg}
module. registration module.
\refstmodindex{pickle} \withsubitem{(copy protocol)}{%
\setindexsubitem{(copy protocol)} \ttindex{__getinitargs__()}%
\ttindex{__getinitargs__} \ttindex{__getstate__()}%
\ttindex{__getstate__} \ttindex{__setstate__()}}
\ttindex{__setstate__}
In order for a class to define its own copy implementation, it can In order for a class to define its own copy implementation, it can
define special methods \method{__copy__()}\ttindex{__copy__} and define special methods \method{__copy__()} and
\method{__deepcopy__()}\ttindex{__deepcopy__}. The former is called to \method{__deepcopy__()}. The former is called to implement the
implement the shallow copy operation; no additional arguments are shallow copy operation; no additional arguments are passed. The
passed. The latter is called to implement the deep copy operation; it latter is called to implement the deep copy operation; it is passed
is passed one argument, the memo dictionary. If the one argument, the memo dictionary. If the \method{__deepcopy__()}
\method{__deepcopy__()} implementation needs to make a deep copy of a implementation needs to make a deep copy of a component, it should
component, it should call the \function{deepcopy()} function with the call the \function{deepcopy()} function with the component as first
component as first argument and the memo dictionary as second argument and the memo dictionary as second argument.
argument. \withsubitem{(copy protocol)}{%
\ttindex{__copy__()}%
\ttindex{__deepcopy__()}}
\begin{seealso}
\seemodule{pickle}{Discussion of the special disciplines used to
support object state retrieval and restoration.}
\end{seealso}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment