[Python-Dev] The iterator story
Greg Ewing
greg@cosc.canterbury.ac.nz
Mon, 22 Jul 2002 12:20:12 +1200 (NZST)
> Should people prefer to write:
>
> for item from iterator:
> do something
>
> when they only need to loop over something once?
This shows up a problem with Ping's proposal, I think:
The place where you write the for-loop isn't the place
where you know whether something will be iterated over
more than once or not. How is a library routine going
to know whether a sequence passed to it is going to
be used again later? It's impossible -- global knowledge
of the whole program is needed.
This appears to leave the library writer with two
choices: (1) Use for-in, to be on the safe side,
in case the user doesn't want the sequence destroyed --
but then it can't be used on a destructive iterator,
even if the caller knows he won't be using it again;
(2) use for-from, and force everyone who calls it to
adapt sequences to iterators before calling.
Either way, things get messy and complicated and
possibly dangerous.
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury, | A citizen of NewZealandCorp, a |
Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. |
greg@cosc.canterbury.ac.nz +--------------------------------------+