[Python-Dev] iterators

Guido van Rossum guido@beopen.com
Mon, 21 Aug 2000 22:32:18 -0500


[BDFL]
> > The statement
> > 
> >   for <variable> in <object>: <block>
> > 
> > should translate into this kind of pseudo-code:
> > 
> >   # variant 1
> >   __temp = <object>.newiterator()
> >   while 1:
> >       try: <variable> = __temp.next()
> >       except ExhaustedIterator: break
> >       <block>
> > 
> > or perhaps (to avoid the relatively expensive exception handling):
> > 
> >   # variant 2
> >   __temp = <object>.newiterator()
> >   while 1:
> >       __flag, <variable = __temp.next()
> >       if not __flag: break
> >       <block>

[MAL]
> How about a third variant:
> 
> #3:
> __iter = <object>.iterator()
> while __iter:
>    <variable> = __iter.next()
>    <block>
> 
> This adds a slot call, but removes the malloc overhead introduced
> by returning a tuple for every iteration (which is likely to be
> a performance problem).

Are you sure the slot call doesn't cause some malloc overhead as well?

Ayway, the problem with this one is that it requires a dynamic
iterator (one that generates values on the fly, e.g. something reading
lines from a pipe) to hold on to the next value between "while __iter"
and "__iter.next()".

> Another possibility would be using an iterator attribute
> to get at the variable:
> 
> #4:
> __iter = <object>.iterator()
> while 1:
>    if not __iter.next():
>         break
>    <variable> = __iter.value
>    <block>

Uglier than any of the others.

> You might want to check out the counterobject.c approach I used
> to speed up the current for-loop in Python 1.5's ceval.c:
> it's basically a mutable C integer which is used instead of
> the current Python integer index.

> The details can be found in my old patch:
> 
>   http://starship.python.net/crew/lemburg/mxPython-1.5.patch.gz

Ah, yes, that's what I was thinking of.

> """ Generic object iterators.
[...]

Thanks -- yes, that's what I was thinking of.  Did you just whip this
up?

--Guido van Rossum (home page: http://www.pythonlabs.com/~guido/)