[Python-Dev] Single- vs. Multi-pass iterability

Delaney, Timothy tdelaney@avaya.com
Mon, 15 Jul 2002 08:46:01 +1000


> From: David Abrahams [mailto:david.abrahams@rcn.com]
> 
> One other possibility: if x.__iter__() is x, it's a 
> single-pass sequence. I
> realize this involves actually invoking the __iter__ method 
> and conjuring
> up a new iterator, but that's generally a lightweight operation...

Definitely not reliable - it will fail for a file object ... (even with the
changes currently going in).

What would be more reliable (but still not infallible) would be:

if iter(x) == iter(x):
    # this is *definitely* a single-pass iterable

All iterators are by definition single-pass iterables, and with the changes
being made to the file object, the above code would work for all builtin
iterable types as well.

Tim Delaney