[Tutor] Re: Iterators (was: Re: text module)

Emile van Sebille emile@fenx.com
Sat, 31 Aug 2002 09:23:31 -0700


Erik:

[snip iter stuff]

> It's a confusing document IMHO.  I would appreciate any discussion and
> thoughts that anyone cares to contribute.
>

Here's a simple example that subclasses list overriding __iter__ to walk the
list backwards.

class Backwards(list):
    def __iter__(self):
        idx = len(self)
        while idx:
            idx-=1
            yield self[idx]

lst = range(10)
for i in lst: print i,

print

lst = Backwards(range(10))
for i in lst: print i,