@@ -199,11 +199,13 @@ foundation for writing functional-style programs: iterators.
199199
200200An iterator is an object representing a stream of data; this object
201201returns the data one element at a time. A Python iterator must
202- support a method called ``next () `` that takes no arguments and always
202+ support a method called ``__next__ () `` that takes no arguments and always
203203returns the next element of the stream. If there are no more elements
204- in the stream, ``next () `` must raise the ``StopIteration `` exception.
204+ in the stream, ``__next__ () `` must raise the ``StopIteration `` exception.
205205Iterators don't have to be finite, though; it's perfectly reasonable
206206to write an iterator that produces an infinite stream of data.
207+ The built-in ``next() `` function is normally used to call the iterator's
208+ ``__next__() `` method.
207209
208210The built-in ``iter() `` function takes an arbitrary object and tries
209211to return an iterator that will return the object's contents or
@@ -218,13 +220,13 @@ You can experiment with the iteration interface manually::
218220 >>> it = iter(L)
219221 >>> print it
220222 <iterator object at 0x8116870>
221- >>> it. next()
223+ >>> next(it )
222224 1
223- >>> it. next()
225+ >>> next(it )
224226 2
225- >>> it. next()
227+ >>> next(it )
226228 3
227- >>> it. next()
229+ >>> next(it )
228230 Traceback (most recent call last):
229231 File "<stdin>", line 1, in ?
230232 StopIteration
@@ -271,7 +273,7 @@ won't return either.
271273Note that you can only go forward in an iterator; there's no way to
272274get the previous element, reset the iterator, or make a copy of it.
273275Iterator objects can optionally provide these additional capabilities,
274- but the iterator protocol only specifies the ``next () `` method.
276+ but the iterator protocol only specifies the ``__next__ () `` method.
275277Functions may therefore consume all of the iterator's output, and if
276278you need to do something different with the same stream, you'll have
277279to create a new iterator.
@@ -485,21 +487,21 @@ outputs the value of ``i``, similar to a ``return``
485487statement. The big difference between ``yield `` and a
486488``return `` statement is that on reaching a ``yield `` the
487489generator's state of execution is suspended and local variables are
488- preserved. On the next call to the generator's `` . next() `` method ,
490+ preserved. On the next call `` next(generator ) ``,
489491the function will resume executing.
490492
491493Here's a sample usage of the ``generate_ints() `` generator::
492494
493495 >>> gen = generate_ints(3)
494496 >>> gen
495497 <generator object at 0x8117f90>
496- >>> gen. next()
498+ >>> next(gen )
497499 0
498- >>> gen. next()
500+ >>> next(gen )
499501 1
500- >>> gen. next()
502+ >>> next(gen )
501503 2
502- >>> gen. next()
504+ >>> next(gen )
503505 Traceback (most recent call last):
504506 File "stdin", line 1, in ?
505507 File "stdin", line 2, in generate_ints
@@ -521,7 +523,7 @@ You could achieve the effect of generators manually by writing your
521523own class and storing all the local variables of the generator as
522524instance variables. For example, returning a list of integers could
523525be done by setting ``self.count `` to 0, and having the
524- ``next () `` method increment ``self.count `` and return it.
526+ ``__next__ () `` method increment ``self.count `` and return it.
525527However, for a moderately complicated generator, writing a
526528corresponding class can be much messier.
527529
@@ -583,7 +585,7 @@ use parentheses when there's an operation, as in ``val = (yield i)
583585Values are sent into a generator by calling its
584586``send(value) `` method. This method resumes the
585587generator's code and the ``yield `` expression returns the specified
586- value. If the regular ``next () `` method is called, the
588+ value. If the regular ``__next__ () `` method is called, the
587589``yield `` returns ``None ``.
588590
589591Here's a simple counter that increments by 1 and allows changing the
@@ -604,18 +606,18 @@ value of the internal counter.
604606And here's an example of changing the counter:
605607
606608 >>> it = counter(10 )
607- >>> print it. next()
609+ >>> print next (it )
608610 0
609- >>> print it. next()
611+ >>> print next (it )
610612 1
611613 >>> print it.send(8 )
612614 8
613- >>> print it. next()
615+ >>> print next (it )
614616 9
615- >>> print it. next()
617+ >>> print next (it )
616618 Traceback (most recent call last):
617619 File ``t.py'', line 15, in ?
618- print it. next()
620+ print next(it )
619621 StopIteration
620622
621623Because ``yield `` will often be returning ``None ``, you
0 commit comments