diff options
author | Georg Brandl <georg@python.org> | 2007-04-21 15:47:16 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2007-04-21 15:47:16 (GMT) |
commit | a18af4e7a2091d11478754eb66ae387a85535763 (patch) | |
tree | fea8015d656cfee937bb6f3d106e6ca0e9f19d78 /Doc/tut | |
parent | 4d2adcca52ced412d4bdf131b872729c43520d58 (diff) | |
download | cpython-a18af4e7a2091d11478754eb66ae387a85535763.zip cpython-a18af4e7a2091d11478754eb66ae387a85535763.tar.gz cpython-a18af4e7a2091d11478754eb66ae387a85535763.tar.bz2 |
PEP 3114: rename .next() to .__next__() and add next() builtin.
Diffstat (limited to 'Doc/tut')
-rw-r--r-- | Doc/tut/glossary.tex | 6 | ||||
-rw-r--r-- | Doc/tut/tut.tex | 27 |
2 files changed, 17 insertions, 16 deletions
diff --git a/Doc/tut/glossary.tex b/Doc/tut/glossary.tex index 738e12d..a19416b 100644 --- a/Doc/tut/glossary.tex +++ b/Doc/tut/glossary.tex @@ -117,7 +117,7 @@ contain one or more {}\keyword{for} or \keyword{while} loops that \keyword{yield} elements back to the caller. The function execution is stopped at the {}\keyword{yield} keyword (returning the result) and is resumed there when the next element is requested by calling the -\method{next()} method of the returned iterator. +\method{__next__()} method of the returned iterator. \index{generator expression} \item[generator expression] @@ -207,10 +207,10 @@ hold the iterator for the duration of the loop. See also \index{iterator} \item[iterator] An object representing a stream of data. Repeated calls to the -iterator's \method{next()} method return successive items in the +iterator's \method{__next__()} method return successive items in the stream. When no more data is available a \exception{StopIteration} exception is raised instead. At this point, the iterator object is -exhausted and any further calls to its \method{next()} method just +exhausted and any further calls to its \method{__next__()} method just raise \exception{StopIteration} again. Iterators are required to have an \method{__iter__()} method that returns the iterator object itself so every iterator is also iterable and may be used in most diff --git a/Doc/tut/tut.tex b/Doc/tut/tut.tex index a0b75c6..4abd0bd 100644 --- a/Doc/tut/tut.tex +++ b/Doc/tut/tut.tex @@ -4488,34 +4488,35 @@ This style of access is clear, concise, and convenient. The use of iterators pervades and unifies Python. Behind the scenes, the \keyword{for} statement calls \function{iter()} on the container object. The function returns an iterator object that defines the method -\method{next()} which accesses elements in the container one at a -time. When there are no more elements, \method{next()} raises a +\method{__next__()} which accesses elements in the container one at a +time. When there are no more elements, \method{__next__()} raises a \exception{StopIteration} exception which tells the \keyword{for} loop -to terminate. This example shows how it all works: +to terminate. You can call the \method{__next__()} method using the +\function{next()} builtin; this example shows how it all works: \begin{verbatim} >>> s = 'abc' >>> it = iter(s) >>> it <iterator object at 0x00A1DB50> ->>> it.next() +>>> next(it) 'a' ->>> it.next() +>>> next(it) 'b' ->>> it.next() +>>> next(it) 'c' ->>> it.next() +>>> next(it) Traceback (most recent call last): File "<stdin>", line 1, in ? - it.next() + next(it) StopIteration \end{verbatim} Having seen the mechanics behind the iterator protocol, it is easy to add iterator behavior to your classes. Define a \method{__iter__()} method -which returns an object with a \method{next()} method. If the class defines -\method{next()}, then \method{__iter__()} can just return \code{self}: +which returns an object with a \method{__next__()} method. If the class defines +\method{__next__()}, then \method{__iter__()} can just return \code{self}: \begin{verbatim} class Reverse: @@ -4525,7 +4526,7 @@ class Reverse: self.index = len(data) def __iter__(self): return self - def next(self): + def __next__(self): if self.index == 0: raise StopIteration self.index = self.index - 1 @@ -4545,7 +4546,7 @@ s Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the \keyword{yield} statement whenever -they want to return data. Each time \method{next()} is called, the +they want to return data. Each time \function{next()} is called on it, the generator resumes where it left-off (it remembers all the data values and which statement was last executed). An example shows that generators can be trivially easy to create: @@ -4566,7 +4567,7 @@ g Anything that can be done with generators can also be done with class based iterators as described in the previous section. What makes generators so -compact is that the \method{__iter__()} and \method{next()} methods are +compact is that the \method{__iter__()} and \method{__next__()} methods are created automatically. Another key feature is that the local variables and execution state |