page137.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 70 行
HTML
70 行
<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="tex2html2788" HREF="page138.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2786" HREF="page132.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2782" HREF="page136.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2790" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION006115000000000000000"><tt>__iter__</tt> Method</A></H3><P>As discussed in Section <A HREF="page121.html#secadtsiterators"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>,the <tt>__iter__</tt> method of a <tt>Container</tt>returns an <tt>Iterator</tt>.In Python, an iterator is implicitly used when you write a <tt>for</tt> loop like this:<PRE>stack = new StackAsArray(57)stack.push(3)stack.push(1)stack.push(4)for obj in stack: print obj</PRE>The Python <tt>for</tt> loop given aboveis implemented as if it was coded like this:<PRE>it = iter(stack) # Equivalent to stack.__iter__()while true: try: obj = it.next() print obj except StopIteration: break</PRE>This code creates an instance of the <tt>StackAsArray</tt> classand assigns it to the variable <tt>stack</tt>.Next, several <tt>int</tt>s are pushed onto the stack.Finally, an iterator is used to systematically print out allof the objects in the stack.<P>Program <A HREF="page137.html#progstackAsArrayd"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the <tt>__iter__</tt> methodof the <tt>StackAsArray</tt> class.The <tt>__iter__</tt> method returns a new instanceof the nested class <tt>StackAsArray.Iterator</tt>that implements the iterator protocol (lines 3-14).<P><P><A NAME="5648"> </A><A NAME="progstackAsArrayd"> </A> <IMG WIDTH=575 HEIGHT=371 ALIGN=BOTTOM ALT="program5362" SRC="img669.gif" ><BR><STRONG>Program:</STRONG> <tt>StackAsArray</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 array of the current object.<P>The <tt>_next</tt> method is called in the body of the loop to advance the iterator to the next object in the stack.The <tt>_next</tt> method increments the <tt>_position</tt> instance attributeand then returns the object in the stack specified at that position.The <tt>_next</tt> method resets the position to -1 andraises a <tt>StopIteration</tt> exception when there are not more elements.Clearly, the running time of <tt>_next</tt> is <I>O</I>(1).<P><HR><A NAME="tex2html2788" HREF="page138.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2786" HREF="page132.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2782" HREF="page136.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2790" 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 + -
显示快捷键?