page143.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 45 行
HTML
45 行
<HTML><HEAD><TITLE>__iter__ 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="tex2html2857" HREF="page144.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2855" HREF="page138.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2851" HREF="page142.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2859" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION006125000000000000000"><tt>__iter__</tt> Method</A></H3><P>Program <A HREF="page143.html#progstackAsLinkedListd"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>__iter__</tt> methodof the <tt>StackAsLinkedList</tt> class.The <tt>__iter__</tt> method returns an instanceof the nested class <tt>StackAsLinkedlist.Iterator</tt>that implements the iterator protocol (lines 3-16).<P><P><A NAME="5667"> </A><A NAME="progstackAsLinkedListd"> </A> <IMG WIDTH=575 HEIGHT=390 ALIGN=BOTTOM ALT="program5513" SRC="img673.gif" ><BR><STRONG>Program:</STRONG> <tt>StackAsLinkedList</tt> class <tt>__iter__</tt> method.<BR><P><P>The <tt>Iterator</tt> class has two instance attributes,<tt>_container</tt> and <tt>_position</tt>.The <tt>_container</tt> instance attribute refers to the stackwhose elements are being enumerated.The <tt>_position</tt> instance attribute is usedto keep track of the position in the linked list of the nextobject to be enumerated.<P>The purpose of the <tt>_next</tt> method isto advance the iterator to the next object in the stackand to raise a <tt>StopIteration</tt> exceptionwhen there are no more elements to be enumerated.In Program <A HREF="page143.html#progstackAsLinkedListd"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> elements remain as long asthe <tt>_position</tt> is not <tt>None</tt>.Clearly, the running time of <tt>_next</tt> is <I>O</I>(1).<P><HR><A NAME="tex2html2857" HREF="page144.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2855" HREF="page138.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2851" HREF="page142.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2859" 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 + =
减小字号Ctrl + -
显示快捷键?