Mercurial > cpython-lru_cache-weakref
comparison Doc/tutorial/classes.rst @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | 0cddf0bd19f8 |
| children | 65a3ba21c679 |
comparison
equal
deleted
inserted
replaced
| 79688:3534acfa8e37 | 79691:05c8928362ee |
|---|---|
| 735 print(line) | 735 print(line) |
| 736 | 736 |
| 737 This style of access is clear, concise, and convenient. The use of iterators | 737 This style of access is clear, concise, and convenient. The use of iterators |
| 738 pervades and unifies Python. Behind the scenes, the :keyword:`for` statement | 738 pervades and unifies Python. Behind the scenes, the :keyword:`for` statement |
| 739 calls :func:`iter` on the container object. The function returns an iterator | 739 calls :func:`iter` on the container object. The function returns an iterator |
| 740 object that defines the method :meth:`__next__` which accesses elements in the | 740 object that defines the method :meth:`~iterator.__next__` which accesses |
| 741 container one at a time. When there are no more elements, :meth:`__next__` | 741 elements in the container one at a time. When there are no more elements, |
| 742 raises a :exc:`StopIteration` exception which tells the :keyword:`for` loop to | 742 :meth:`__next__` raises a :exc:`StopIteration` exception which tells the |
| 743 terminate. You can call the :meth:`__next__` method using the :func:`next` | 743 :keyword:`for` loop to terminate. You can call the :meth:`__next__` method |
| 744 built-in function; this example shows how it all works:: | 744 using the :func:`next` built-in function; this example shows how it all works:: |
| 745 | 745 |
| 746 >>> s = 'abc' | 746 >>> s = 'abc' |
| 747 >>> it = iter(s) | 747 >>> it = iter(s) |
| 748 >>> it | 748 >>> it |
| 749 <iterator object at 0x00A1DB50> | 749 <iterator object at 0x00A1DB50> |
| 759 next(it) | 759 next(it) |
| 760 StopIteration | 760 StopIteration |
| 761 | 761 |
| 762 Having seen the mechanics behind the iterator protocol, it is easy to add | 762 Having seen the mechanics behind the iterator protocol, it is easy to add |
| 763 iterator behavior to your classes. Define an :meth:`__iter__` method which | 763 iterator behavior to your classes. Define an :meth:`__iter__` method which |
| 764 returns an object with a :meth:`__next__` method. If the class defines | 764 returns an object with a :meth:`~iterator.__next__` method. If the class |
| 765 :meth:`__next__`, then :meth:`__iter__` can just return ``self``:: | 765 defines :meth:`__next__`, then :meth:`__iter__` can just return ``self``:: |
| 766 | 766 |
| 767 class Reverse: | 767 class Reverse: |
| 768 """Iterator for looping over a sequence backwards.""" | 768 """Iterator for looping over a sequence backwards.""" |
| 769 def __init__(self, data): | 769 def __init__(self, data): |
| 770 self.data = data | 770 self.data = data |
| 817 o | 817 o |
| 818 g | 818 g |
| 819 | 819 |
| 820 Anything that can be done with generators can also be done with class based | 820 Anything that can be done with generators can also be done with class based |
| 821 iterators as described in the previous section. What makes generators so | 821 iterators as described in the previous section. What makes generators so |
| 822 compact is that the :meth:`__iter__` and :meth:`__next__` methods are created | 822 compact is that the :meth:`__iter__` and :meth:`~generator.__next__` methods |
| 823 automatically. | 823 are created automatically. |
| 824 | 824 |
| 825 Another key feature is that the local variables and execution state are | 825 Another key feature is that the local variables and execution state are |
| 826 automatically saved between calls. This made the function easier to write and | 826 automatically saved between calls. This made the function easier to write and |
| 827 much more clear than an approach using instance variables like ``self.index`` | 827 much more clear than an approach using instance variables like ``self.index`` |
| 828 and ``self.data``. | 828 and ``self.data``. |
