📄 page121.html
字号:
<HTML><HEAD><TITLE>Iterators</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="tex2html2606" HREF="page122.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2604" HREF="page113.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2598" HREF="page120.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2608" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION005240000000000000000">Iterators</A></H2><A NAME="secadtsiterators"> </A><P>In this section we introduce an abstraction calledan <em>iterator</em><A NAME=4585> </A>.An iterator provides the means to access one-by-one all theobjects in a container.<P>A Python iterator is any classthat provides two methods called <tt>__iter__</tt> and <tt>next</tt>and that implements the <em>iterator protocol</em><A NAME=4589> </A>which we describe below.In order to use an iterator to access the objects in a container,that container must provide iteration support.Specifically, that class must implement a method called <tt>__iter__</tt>that returns the corresponding iterator for that class.<P>The idea is that for every concrete container classderived from the <tt>Container</tt> base classwe will also implement a related classderived from the abstract <tt>Iterator</tt> class shown in Program <A HREF="page121.html#progiteratora"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A><P><P><A NAME="4961"> </A><A NAME="progiteratora"> </A> <IMG WIDTH=575 HEIGHT=275 ALIGN=BOTTOM ALT="program4594" SRC="img649.gif" ><BR><STRONG>Program:</STRONG> <tt>Iterator</tt> class.<BR><P><P>The <tt>Iterator</tt> class comprises three methods,<tt>__init__</tt>, <tt>__iter__</tt>, and <tt>next</tt>.The <tt>__init__</tt> and <tt>__iter__</tt> methods are straightforward.The abstract <tt>next</tt> method provided in a derived classis required to implement the iterator protocol.<P>In order to understand the iterator protocol,it is best to consider first an example which illustratesthe use of an iterator.Consider a concrete container class, say <tt>SomeContainer</tt>,that implements the <tt>Container</tt> interface.The following code fragment illustrates the use of an iteratorto access one-by-one the objects contained in the container:<PRE>c = SomeContainer()# ...i = iter(c) # Equivalent to c.__iter__()while True: try: obj = i.next() print obj except StopIteration: break</PRE><P>In order to have the desired effect,the <tt>next</tt> method must behave as follows:<OL><LI> Each time the <tt>next</tt> method is called, it returns the next element in the container.<LI> When all the elements in the container have been returned, the next method raises the <tt>StopIteration</tt> exception.</OL>Given these semantics for the <tt>next</tt> method,the program fragment shown above systematically visitsall of the objects in the containerand prints each one on its own line of the console.<P>A certain amount of care is required when defining and using iterators.In order to simplify the implementation of iterators,we shall assume that while an iterator is in use,the associated container will not be modified.<P><BR> <HR><UL> <LI> <A NAME="tex2html2609" HREF="page122.html#SECTION005241000000000000000">Iterators and the Python <tt>for</tt> statement</A></UL><HR><A NAME="tex2html2606" HREF="page122.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html2604" HREF="page113.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html2598" HREF="page120.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html2608" 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 + -