do loop

B

beliavsky

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

In Fortran 90 and 95, the analogous code

do i=0,2
i = i*5
print*,i
end do

is illegal, because variable 'i' cannot be changed inside the loop.
The constraint of not allowing the loop variable to change within the
body of the loop can prevent errors in logic when the body of the loop
is large.

Is there a way to write a loop in Python that enforces this
constraint? Should such functionality be added to the language?
 
P

Peter Hansen

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

In Fortran 90 and 95, the analogous code

do i=0,2
i = i*5
print*,i
end do

is illegal, because variable 'i' cannot be changed inside the loop.
The constraint of not allowing the loop variable to change within the
body of the loop can prevent errors in logic when the body of the loop
is large.

Is there a way to write a loop in Python that enforces this
constraint? Should such functionality be added to the language?

No, absolutely not. I don't believe this is a common error,
and in fact sometimes even in e.g. FORTRAN or BASIC you *wanted*
to change the loop variable to affect the sequence. In any case
Python's typical approach to such things is to treat the programmer
as an adult.

Note that in Python you can trust that such a change will not affect
the sequence as it would with some other languages, but if you do
*want* to change the loop variable, and have it affect the sequence,
you can use a while loop instead.

-Peter
 
F

Francis Avila

(e-mail address removed) wrote in message
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.
 
K

Kristian Ovaska

(e-mail address removed):
In a Python 'for' loop, one can change the value of looping variable, [...]
Is there a way to write a loop in Python that enforces this
constraint? Should such functionality be added to the language?

In Python, "for i in range(3)" produces a sequence [0, 1, 2] and binds
each element to i in turn. It's the sequence that drives the loop; i
is more like a placeholder, and you are free to bind some other value
to it if you wish - it doesn't change loop semantics. So, it makes no
sense to restrict i.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,944
Members
47,492
Latest member
gabbywilliam

Latest Threads

Top