473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

do loop

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?
Jul 18 '05 #1
3 6457
be*******@aol.com wrote:

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
Jul 18 '05 #2
be*******@aol.com wrote in message
<30**************************@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

Jul 18 '05 #3
be*******@aol.com:
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.

--
Kristian Ovaska - http://www.cs.helsinki.fi/u/hkovaska/en/
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.