[Python-ideas] Introduce collections.Reiterable
Terry Reedy
tjreedy at udel.edu
Tue Sep 24 22:39:28 CEST 2013
On 9/24/2013 12:21 AM, Steven D'Aprano wrote:
> Maybe it's the mathematician in me speaking, but I don't think very many
> unbounded iterators are found outside of maths sequences.
Perhaps you are confusing 'actual infinity' or mathematics with the
potential infinity of iterators. Unbound, or more exactly, potentially
unbounded iterators are quite common.
First, many source iterators based on external sources are or are
potentially unbounded. For example, text-mode files are text line
iterators. Files based on finite disk files are bounded, but others
(based on keyboard, socket, or other input channels) may not be.
Consider the following example (simplified, like all examples, for
illustrative purposes).
def source(prompt):
"Yield user responses to prompt."
while True:
yield input(prompt))
# Even if 'quit' were recognized and turned into StopIteration,
# it still might never happen.
or
def measures(read_instrument):
"Yield values returned by read_instrument."
while True:
yield read_instrument()
A queue can yield an unbounded sequence even if it is always finite and
even it it has a maximum size, perhaps because the pool of potential
queue members is finite.
Second, many transform iterators are unbounded if the input iterable is
unbounded.
def transform(func, iterable):
for item in iterable:
try:
yield func(item)
except ValueError:
pass
for i in transform(int, source('Enter an integer: ')):
# process unbounded stream of ints.
Filter, map, and some itertools potentially produce infinite iterators.
Itertools.islice turns infinite iterables finite.
Itertools.cycle turns finite iterables infinite.
At the highest level, interactive apps, including OSes, usually process
indefinite streams of user-generated events.
> After all,
> even if you were to iterate over every atom in the universe, that would
> be bounded, and quite small compared to some of the numbers
> mathematicians deal with... :-)
The atoms of the universe can be reused over and over again in the same
or different combinations to keep the iteration going indefinitely.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list