[Python-Dev] Shall I start adding iterators to Python 2.2?

M.-A. Lemburg mal@lemburg.com
Fri, 20 Apr 2001 11:02:09 +0200


Guido van Rossum wrote:
> 
> Brief overview of what I've got implemented:
> 
> - There is a new built-in operation, spelled as iter(obj) in Python
>   and as PyObject_GetIter(obj) in C; it calls the tp_iter slot in
>   obj's type.  This returns an iterator, which can be any object that
>   implements the iterator protocol.  The iterator protocol defines one
>   method, next(), which returns the next value or raises the new
>   StopIteration exception.

Could we also have a C slot for .next() ? (Python function calls
are way too expensive for these things, IMHO)

Will there be a __iter__() method for Python instances to implement ?

> - For backwards compatibility, if obj's type does not have a valid
>   tp_iter slot, iter(obj) and PyObject_GetIter(obj) create a sequence
>   iterator that iterates over a sequence.
> 
> - "for x in S: B" is implemented roughly as
> 
>   __iter = iter(S)
>   while 1:
>     try:
>       x = __iter.next()
>     except StopIteration:
>       break
>     B
> 
>   (except that the semantics of break when there's an else clause are
>   not different from what this Python code would do).
> 
> - The test "key in dict" is implemented as "dict.has_key(key)".  (This
>   was done by implementing the sq_contains slot.

Cool :)
 
> - iter(dict) returns an iterator that iterates over the keys of dict
>   without creating a list of keys first.  This means that "for key in
>   dict" has the same effect as "for key in dict.keys()" as long as the
>   loop body doesn't modify the dictionary (assignment to existing keys
>   is okay).
>
> - There's an operation to create an iterator from a function and a
>   sentinel value.  This is spelled as iter(function, sentinel).  For
>   example,
> 
>     for line in iter(sys.stdin.readline, ""):
>       ...
> 
>   is an efficient loop over the lines of stdin.

Hmm, I guess you have to compare each function output to the
sentinel then, right ? This can be very expensive.

Wouldn't an exception base class also do the trick as sentinel ? The 
iterator would then stop when an exception is raised by the
function which matches the sentinel exception.
 
> - But even cooler is this, which is totally equivalent:
> 
>     for line in sys.stdin:
>       ...
> 
> - Not yet implemented, but part of the plan, is to use iterators for
>   all other implicit loops, like map/reduce/filter, min/max, and the
>   "in" test for sequences that don't define sq_contains.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Company & Consulting:                           http://www.egenix.com/
Python Pages:                           http://www.lemburg.com/python/