⇤ ← Revision 1 as of 2005-04-02 23:58:27
1350
Comment: basics
|
2132
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
An '''iterator''' is an object that implements {{{__iter__}}} and and {{{next}}}. | An '''iterable''' object is an object that implements {{{__iter__}}}. {{{__iter__}}} is expected to return an '''iterator''' object. |
Line 3: | Line 3: |
You can use them in for loops, and you can use them to construct lists. | An '''iterator''' is an object that implements {{{next}}}. {{{next}}} is expected to return the next element of the iterable object that returned it, and raise a {{{StopIteration}}} exception when no more elements are available. In the simplest case the iterable will implement {{{next}}} itself and return {{{self}}} in {{{__iter__}}}. You can use iterables in for loops, and you can use them to construct lists. |
Line 13: | Line 17: |
class RandomIterator: | class RandomIterable: |
Line 24: | Line 28: |
'''A:''' This is so that the iterator can be used in a for...in loop. | '''A:''' This is a very simple case. More complex iterables may very well return separate iterator objects. '''Q:''' When would I need an extra iterator? '''A:''' Iterator will typically need to maintain some kind of position state information (like the index of the last element returned or the like). If the iterable maintained that state itself, it would become inherently non-reentrant (meaning you could use it only one loop at a time). |
Line 28: | Line 36: |
for eggs in RandomIterator(): | for eggs in RandomIterable(): |
Line 36: | Line 44: |
>>> list(RandomIterator()) | >>> list(RandomIterable()) |
Line 38: | Line 46: |
>>> list(RandomIterator()) | >>> list(RandomIterable()) |
Line 40: | Line 48: |
>>> list(RandomIterator()) | >>> list(RandomIterable()) |
Line 42: | Line 50: |
>>> list(RandomIterator()) | >>> list(RandomIterable()) |
Line 48: | Line 56: |
An object isn't an iterator unless it provides ''both'' methods. If it ''does'' provide these methods, then it's an iterator. | An object isn't iterable unless it provides {{{__iter__}}}. And for an object to be a valid iterator, it must provide {{{next}}}. |
An iterable object is an object that implements __iter__. __iter__ is expected to return an iterator object.
An iterator is an object that implements next. next is expected to return the next element of the iterable object that returned it, and raise a StopIteration exception when no more elements are available.
In the simplest case the iterable will implement next itself and return self in __iter__.
You can use iterables in for loops, and you can use them to construct lists.
Example Iterator
Here is an iterator that returns a random number of 1's:
Q: Why is __iter__ there, if it just returns self?
A: This is a very simple case. More complex iterables may very well return separate iterator objects.
Q: When would I need an extra iterator?
A: Iterator will typically need to maintain some kind of position state information (like the index of the last element returned or the like). If the iterable maintained that state itself, it would become inherently non-reentrant (meaning you could use it only one loop at a time).
You can also use it in list construction:
...both of these uses require __iter__.
An object isn't iterable unless it provides __iter__. And for an object to be a valid iterator, it must provide next.
Links
[http://www.python.org/peps/pep-0234.html PEP-234: Iterators]
[http://www-106.ibm.com/developerworks/library/l-pycon.html?n-l-9271 Iterators & Simple Generators]
See also: ["Generators"]
Discussion
- (none yet!)