@@ -184,11 +184,11 @@ foundation for writing functional-style programs: iterators.
184184
185185An iterator is an object representing a stream of data; this object returns the
186186data one element at a time. A Python iterator must support a method called
187- ``next () `` that takes no arguments and always returns the next element of the
188- stream. If there are no more elements in the stream, ``next () `` must raise the
189- ``StopIteration `` exception. Iterators don't have to be finite, though; it's
190- perfectly reasonable to write an iterator that produces an infinite stream of
191- data.
187+ ``__next__ () `` that takes no arguments and always returns the next element of
188+ the stream. If there are no more elements in the stream, ``__next__ () `` must
189+ raise the ``StopIteration `` exception. Iterators don't have to be finite,
190+ though; it's perfectly reasonable to write an iterator that produces an infinite
191+ stream of data.
192192
193193The built-in :func: `iter ` function takes an arbitrary object and tries to return
194194an iterator that will return the object's contents or elements, raising
@@ -203,13 +203,13 @@ You can experiment with the iteration interface manually:
203203 >>> it = iter (L)
204204 >>> it
205205 <...iterator object at ...>
206- >>> it.next ()
206+ >>> it.__next__ ()
207207 1
208- >>> it. next()
208+ >>> next (it )
209209 2
210- >>> it. next()
210+ >>> next (it )
211211 3
212- >>> it. next()
212+ >>> next (it )
213213 Traceback (most recent call last):
214214 File "<stdin>", line 1, in ?
215215 StopIteration
@@ -467,20 +467,20 @@ the ``yield`` expression, the generator outputs the value of ``i``, similar to a
467467``return `` statement. The big difference between ``yield `` and a ``return ``
468468statement is that on reaching a ``yield `` the generator's state of execution is
469469suspended and local variables are preserved. On the next call to the
470- generator's ``.next () `` method, the function will resume executing.
470+ generator's ``.__next__ () `` method, the function will resume executing.
471471
472472Here's a sample usage of the ``generate_ints() `` generator:
473473
474474 >>> gen = generate_ints(3 )
475475 >>> gen
476476 <generator object at ...>
477- >>> gen. next()
477+ >>> next (gen )
478478 0
479- >>> gen. next()
479+ >>> next (gen )
480480 1
481- >>> gen. next()
481+ >>> next (gen )
482482 2
483- >>> gen. next()
483+ >>> next (gen )
484484 Traceback (most recent call last):
485485 File "stdin", line 1, in ?
486486 File "stdin", line 2, in generate_ints
@@ -500,7 +500,7 @@ the bottom of the function.
500500You could achieve the effect of generators manually by writing your own class
501501and storing all the local variables of the generator as instance variables. For
502502example, returning a list of integers could be done by setting ``self.count `` to
503- 0, and having the ``next () `` method increment ``self.count `` and return it.
503+ 0, and having the ``__next__ () `` method increment ``self.count `` and return it.
504504However, for a moderately complicated generator, writing a corresponding class
505505can be much messier.
506506
@@ -555,7 +555,7 @@ but have to use parentheses when there's an operation, as in ``val = (yield i)
555555
556556Values are sent into a generator by calling its ``send(value) `` method. This
557557method resumes the generator's code and the ``yield `` expression returns the
558- specified value. If the regular ``next () `` method is called, the ``yield ``
558+ specified value. If the regular ``__next__ () `` method is called, the ``yield ``
559559returns ``None ``.
560560
561561Here's a simple counter that increments by 1 and allows changing the value of
@@ -576,15 +576,15 @@ the internal counter.
576576And here's an example of changing the counter:
577577
578578 >>> it = counter(10 )
579- >>> it. next()
579+ >>> next (it )
580580 0
581- >>> it. next()
581+ >>> next (it )
582582 1
583583 >>> it.send(8 )
584584 8
585- >>> it. next()
585+ >>> next (it )
586586 9
587- >>> it. next()
587+ >>> next (it )
588588 Traceback (most recent call last):
589589 File ``t.py'', line 15, in ?
590590 it.next()
0 commit comments