RE: [Python-Dev] Single- vs. Multi-pass iterability
15 Jul
2002
15 Jul
'02
4:16 a.m.
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
7951
Age (days ago)
7951
Last active (days ago)
0 comments
1 participants
participants (1)
-
Delaney, Timothy