recognizing empty iterators

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Fri Jul 25 00:55:27 EDT 2003


> From: Raymond Hettinger [mailto:vze4rx4y at verizon.net]
>
> Yes, it was.  So, Guido decreed that once a iterator has returned
> StopIteration, it will continue to do so on subsequent calls to
> it.next().  Promptly, all of Python's iterators were modified to
> comply.  IOW, iterators can and do stay "permanently exhausted".

Yes - these are the rules, and all the standard iterators conform to them. This is a Good Thing(TM).

A non-standard iterator *may* break the rules (as with just about anything in Python). That would, however, be a Bad Thing(TM).

BTW I was there during those discussions (which is why the thought popped into my mind ...). I can't remember off the top of my head though if iter() is guaranteed to return an iterator that once exhausted will be permanently exhausted (which would probably require a proxy). I'm pretty certain though the the resolution was "the standard iterators will do the Right Thing(TM) and it's up to people to play nice with their own iterators".

> However, Tim is essentially correct in focusing on the "just-in-time"
> nature of iterators.  Part of the core concept for iterators 
> is to *not* know what lies ahead.

Why, thank you sir ;)

Tim Delaney





More information about the Python-list mailing list