page83.html
字号:
<HTML><HEAD><TITLE>Extending Python Lists - An Array Class</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="tex2html2167" HREF="page84.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2165" HREF="page82.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2159" HREF="page82.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2169" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION004110000000000000000">Extending Python Lists - An Array Class</A></H2><P><A NAME="secfdsarrayimp"> </A><P>While the Python programming language does indeed provide built-insupport for lists,that support is not without its shortcomings:For example,the first (or leftmost) index of a list is always zero (or -<I>n</I>)and the rightmost index of a list is always <I>n</I>-1 (or -1).However, in certain applications,it is desireable to index list elements starting with a non-zero base index.Another issue arise from the fact that negative indices are allowed.In many applications, only non-negative indices are requiredand an attempt to use a negative index is evidence of a programming error.Because Python allows negative indices,such errors are not detected at run timeand may lead to incorrect program execution.<P>One way to address these deficiencies is todefine a new class with the desired functionality.We do this by defining an <tt>Array</tt> class with two instance attributes,<tt>_data</tt> and <tt>_baseIndex</tt>.The first is a Python listand the second is a plain integer which recordsthe lower bound for array indices.<P><HR><A NAME="tex2html2167" HREF="page84.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2165" HREF="page82.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2159" HREF="page82.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2169" 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 + -