page84.html

来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 47 行

HTML
47
字号
<HTML><HEAD><TITLE>__init__ 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="tex2html2178" HREF="page85.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2176" HREF="page82.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2170" HREF="page83.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2180" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION004120000000000000000"><tt>__init__</tt> Method</A></H2><A NAME="secfdsarraycons">&#160;</A><P>Program&nbsp;<A HREF="page84.html#progarraya"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> gives the codefor the <tt>Array</tt> class <tt>__init__</tt> method.The <tt>__init__</tt> method takes three arguments,<tt>self</tt>, <tt>length</tt> and <tt>baseIndex</tt>.The <tt>length</tt> argument gives the desired array lengthand the <tt>baseIndex</tt> argument gives the lower bound for array indices.The <tt>__init__</tt> method creates list of of the desired lengthand then sets the <tt>_baseIndex</tt>.Note that the default base index is zeroand the default array length is zero.<P><P><A NAME="2892">&#160;</A><A NAME="progarraya">&#160;</A> <IMG WIDTH=575 HEIGHT=161 ALIGN=BOTTOM ALT="program2707" SRC="img584.gif"  ><BR><STRONG>Program:</STRONG> <tt>Array</tt> class <tt>__init__</tt> method.<BR><P><P>In Python, when a list is allocated, two things happen.First, memory is allocated for the list object and its elements.Second, each element of the list is initializedwith the appropriate default value(in this case all of the list elements refer to the <tt>None</tt> object).<P>For now, we shall assume that the first step takes a constant amount of time.Since there are  <IMG WIDTH=82 HEIGHT=21 ALIGN=MIDDLE ALT="tex2html_wrap_inline60415" SRC="img585.gif"  > elements to be initialized,the second step takes <I>O</I>(<I>n</I>) time.Therefore, the running time ofthe <tt>Array</tt> class <tt>__init__</tt> method is <I>O</I>(<I>n</I>).<P><HR><A NAME="tex2html2178" HREF="page85.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2176" HREF="page82.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2170" HREF="page83.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html2180" 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 &#169; 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 + =
减小字号Ctrl + -
显示快捷键?