[Python-Dev] Single- vs. Multi-pass iterability
Tim Peters
tim.one@comcast.net
Fri, 12 Jul 2002 01:39:47 -0400
[David Abrahams]
> The real reason to be able to introspect is so that you can handle both
> kinds. Even if you're willing to destroy the data by examining it, if
> you know you have a single-pass sequence, you might need to copy its
> elements into a multi-pass sequence (e.g. file.lines()) in order to get
> your work done.
Note that Python uses PySequence_Fast() internally in such cases. This does
whatever it takes to turn an iterable object into something that can be
indexed at random via PySequence_Fast_GET_ITEM(fastseq, int_index). Under
the covers it leaves lists and tuples alone, and materializes everything
else into a temp tuple. I haven't felt a need for something fancier than
that in practice; the lack of participation in this thread from other
old-timers suggests they haven't either (piling on more protocols would
allow to optimize some cases, but it's not clear such cases are important
enough in Python Life to be worth the bother).