Idea: For From

Fredrik Lundh | June 2007

The for-from statement is an alternative to for-in-range, which exposes the internal loop variable, instead of hiding it inside an iterator object.

The main purpose of for-from is to make it easier to describe and understand algorithms. It’s also likely that a Python implementation can use an explicit distinction between looping over an iterable and looping over an integer range to speed things up.

for target from expression to expression:
   suite
[ else:
   suite ]

Notes

from is already a reserved keyword. to and step (see below) are “soft” keywords (similar to as).

Should the step size be implied by the relation between the from and to expressions, or should it default to 1, and be overridable with a step clause?

Should the to value be included in the range, or not?

Should there be a target-less syntax alternative? Or is that better left to the code generator (determining whether a local variable is actually used inside a function is a fairly trivial operation).

 

A Django site. rendered by a django application. hosted by webfaction.