do loop

Francis Avila francisgavila at yahoo.com
Fri Jan 16 09:03:06 EST 2004


beliavsky at aol.com wrote in message
<3064b51d.0401160536.5d80fa97 at posting.google.com>...
>In a Python 'for' loop, one can change the value of looping variable,
>so that
>
>for i in range(3):
>    i = i*5
>    print i
>
>is legal code
>
...
>Is there a way to write a loop in Python that enforces this
>constraint? Should such functionality be added to the language?

Note: I haven't done a search of the archives for possible past discussions
of this, which I really should do before responding.

First, I think this is simply not a problem in practice.  Even much more
common errors, such as binding to a misspelled attribute name, are not
checked for.  Python leans very heavily on regression testing, even as a
design methodology, and thus is unconcerned with having these sort of
bondage-and-discipline language features.

Second, PyChecker (which is a static code checker, providing many of the
kinds of checks that people from staticly-typed languages tend to miss)
could probably easily be extended to check for this sort of behavior, if it
doesn't already have it.

Finally, Fortran is often subject to very agressive optimization by the
compiler.  Python optimization, by comparison, is pretty rudamentary (and
there wouldn't be any speed advantage in this case, anyway, given the
bytecodes Python uses).  Thus there may be a very significant incentive for
Fortran to disallow assignment to loop variables, whereas Python has no such
incentive.

--
Francis Avila




More information about the Python-list mailing list