iters on ints? (reducing the need for range/xrange)

James_Althoff at James_Althoff at
Fri Nov 9 00:33:27 CET 2001

for i in 10:

[Rainer and Emile]
I like it.
Sounds good.

If "for i in 10:" was legal, I'ld guess it meant the same thing as
"for i in [10]:" or "for i in (10,):"

A file *IS* a sequence -- although you could just as easily slice it
by characters as lines -- or by any arbitrary object for binary files,
as Pascal does. If the integer 10 suggests any sort of sequence, it's
the singleton sequence containing: 10.

On the other hand, 2.2 takes us away from the concept of "suggesting a
sequence" in the context of for-loops.  Instead, the new for-loops are
*iterator*-protocol based (not *sequence*-protocol based).  They iterate
over something that *is* or *can return* an iterator object.

So under the proposed scenario, if one looks at 10 and asks "if 10 were to
return an iterator, what would that iterator iterate over?", then it seems
that range(10) would be a more *useful* answer than a sequence of one
element 10.


More information about the Python-list mailing list