who uses Python or Ruby, and for what?

Fredrik Lundh fredrik at pythonware.com
Wed May 2 11:49:43 EDT 2001


Thomas Bellman wroter:
>
> > Python has always had iterators, implemented as forward-only
> > sequences.
>
> The iterator was then the integer you passed to __getitem__()?
> Many people would consider that a *very* limited form of
> iterator.

the iterator is the object that responds to __getitem__(lastindex+1),
not the loop variable itself.

(see fileinput for one example).

>
> >             The new iterator model doesn't add anything that
> > you haven't been able to do before; it just makes things a bit
> > easier to implement and use.
>
> The ability to have several independent and concurrent iterators
> for a single sequence object was not there.  At least not if you
> wanted it to be efficient.  And several other things you can do
> with proper iterators were more than "a bit" more difficult to
> implement.

well, a generic "use new-style iterator as forward sequence"
wrapper is 10 lines, so it cannot be that hard...

class Iterator:
    def __init__(self, obj):
        self.obj = obj
    def __getitem__(self, index):
        if self.obj is not None:
            try:
                return self.obj.next()
            except StopIteration:
                self.obj = None
        raise IndexError

> Sure, you could *implement* iterators in Python, but that's
> not the same as there *being* iterators.

maybe not, but for something that doesn't exist, they've sure
served me well over the last six years ;-)

Cheers /F





More information about the Python-list mailing list