📄 page85.html
字号:
<HTML><HEAD><TITLE>__copy__ Method</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html2189" HREF="page86.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2187" HREF="page82.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2181" HREF="page84.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2191" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION004130000000000000000"><tt>__copy__</tt> Method</A></H2><A NAME="secfdsarraycc"> </A><P>Program <A HREF="page85.html#progarrayb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>__copy__</tt> methodof the <tt>Array</tt> class.This method provides a way to create a copy of a given array.Specifically, the <tt>__copy__</tt> method creates a<em>shallow copy</em><A NAME=2728> </A><A NAME=2729> </A><A NAME=2730> </A>A shallow copy creates a copy of the <tt>Array</tt> objectbut does not copy the objects contained in the array.<P><P><A NAME="2896"> </A><A NAME="progarrayb"> </A> <IMG WIDTH=575 HEIGHT=199 ALIGN=BOTTOM ALT="program2732" SRC="img586.gif" ><BR><STRONG>Program:</STRONG> <tt>Array</tt> class <tt>__copy__</tt> method.<BR><P><P>The <tt>__copy__</tt> method is intended to be used in conjunctionwith the Python <tt>copy</tt> module like this:like this:<PRE>from copy import copya = Array(5)b = copy(a)</PRE>The purpose of the <tt>copy</tt> function in the <tt>copy</tt> moduleis to create a shallow copy of an object.If the object provides a <tt>__copy__</tt> method,the <tt>copy</tt> function calls that method to create the copy.<P>Note that after the copy,<tt>a</tt> and <tt>b</tt> refer to distinct <tt>Array</tt> instances.However, the elements of the array are shared.For example,<tt>a[0]</tt> and <tt>b[0]</tt> both refer to the same object instance.<P>Program <A HREF="page85.html#progarrayb"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> shows a simple implementationof the <tt>__copy__</tt> method.To determine its running time,we need to consider carefully the execution of this method.<P>First, a new <tt>Array</tt> instance is created.As discussed above,this operation takes <I>O</I>(<I>n</I>) in the worst case,where <I>n</I> is the new array length.<P>Next, there is a loop which copies one-by-onethe elements of the input array to the newly allocated array.Clearly this operation takes <I>O</I>(<I>n</I>) time to perform.Finally, the <tt>_baseIndex</tt> instance attribute is copied in <I>O</I>(1) time.Altogether, the running time of the <tt>__copy__</tt> method is <I>T</I>(<I>n</I>)=<I>O</I>(<I>n</I>),where <I>n</I> is the size of the array being copied.<P><HR><A NAME="tex2html2189" HREF="page86.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2187" HREF="page82.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2181" HREF="page84.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2191" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -