PEP 276 Simple Iterator for ints
Skip Montanaro
skip at pobox.com
Wed Nov 14 04:07:24 EST 2001
Magnus> I'd love to drop range() or xrange() from my for loops... (I
Magnus> would like some pseudocode-like literal like 0..n myself, but I
Magnus> see the [0:n] PEP has been rejected, so... :|)
The proposed integer iterator would replace the most common range/xrange
usage directly, but wouldn't supplant them completely without some help. Of
course you could twiddle the index at the beginning of the loop to acheive
the desired effect:
for i in 10:
i = -i # for i in range(0, -10, -1)
for i in 10:
i = 2*i # for i in range(0, 10, 2)
for i in 10:
i = i+1 # for i in range(1, 11)
for i in 10:
i = 3*i+1 # for i in range(1, 11, 3)
etc.
Taking that one gruesome step farther, one could envision a further
extension to for loops:
for i=-i in 10:
...
for i=2*i in 10:
...
for i=i+1 in 10:
...
for i=3*i+1 in 10:
...
but I won't even suggest it as more than an idle thought... ;-)
--
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/
More information about the Python-list
mailing list