Fuzzyman wrote:
Iterators are available in python 2.2
class enumerate:
    def __init__(self, inlist):
        self.inlist = inlist
        self.index = 0

    def next(self):
        if self.index >= len(self.inlist): raise StopIteration
        thisone = self.inlist[self.index]
        self.index += 1
        return self.index-1, thisone

    def __iter__(self):
        return self

A simpler version that works with any iterable (not just sequences):

py> class enumerate(object):
...     def __init__(self, iterable):
...         self._next = iter(iterable).next
...         self._index = -1
...     def __iter__(self):
...         return self
...     def next(self):
...         self._index += 1
...         return self._index, self._next()
...
py> enumerate('abcde')
<__main__.enumerate object at 0x011627F0>
py> list(enumerate('abcde'))
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]

STeVe
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to