473,398 Members | 2,088 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,398 software developers and data experts.

PEP-315 ("do" loop)

Two observations about PEP-315:

1. It's clever, addresses a definite "wart", and is syntactically
similar to try/except. But it's syntax seems like an acquired taste to
me.

2. It is a very general construct, which might be what is called for.
But I wonder if most of the time it would be used to accomplish
something like:

while 1:
line = sys.stdin.readline()
if line == "\n":
break

In a neater way? How about, instead, creating an "until" loop:

until line == "\n":
line = sys.stdin.readline()

Would be defined to work exactly like a while loop except the test is
not evaluated the first time through the loop. Wouldn't this be akin to
checking at the end of the loop, while maintaining a more while-ish
syntax?

Is this at all useful or is something of the order of PEP-315 the way
to go?
Jul 18 '05 #1
11 2232
[Wayne Folta]
Two observations about PEP-315: .. . .
In a neater way? How about, instead, creating an "until" loop:

until line == "\n":
line = sys.stdin.readline()

Would be defined to work exactly like a while loop except the test is
not evaluated the first time through the loop. Wouldn't this be akin to
checking at the end of the loop, while maintaining a more while-ish
syntax?


While it works, or should I say "until it doesn't work", there are two
issues. First, this is not the usual meaning of "until" in other
languages where the semantics is like "while" with the condition
inverted. Second, it is much more clear to put the condition at the
point in the program that it gets evaluated (at the end of the loop,
not the beginning).
Raymond Hettinger
Jul 18 '05 #2

"Wayne Folta" <wf****@netmail.to> wrote in message
news:ma*************************************@pytho n.org...
Two observations about PEP-315:

1. It's clever, addresses a definite "wart", and is syntactically similar to
try/except. But it's syntax seems like an acquired taste to me.

2. It is a very general construct, which might be what is called for. But I
wonder if most of the time it would be used to accomplish something like:

while 1:
line = sys.stdin.readline()
if line == "\n":
break

In a neater way? How about, instead, creating an "until" loop:

until line == "\n":
line = sys.stdin.readline()

Would be defined to work exactly like a while loop except the test is not
evaluated the first time through the loop. Wouldn't this be akin to checking
at the end of the loop, while maintaining a more while-ish syntax?

Is this at all useful or is something of the order of PEP-315 the way to go?

[response]

1. I've reindented your examples - they don't indent properly under some
browser.

2. Please use plain text - the reason your message isn't quoted properly is
that you used something else, and my news client breaks the formatting.

Now to the substantive responses.

1. The proposed syntax breaks Python's indentation scheme in a manner that
try-except, for-else, while-else, and if-elif-else do not.

2. What I find more serious is that there are a huge number of ways to go in
"improving" the basic while loop, none of which seem to be a natural
stopping point. Python's philosophy seems to be to find the "sweet spot"
where you gain the most leverage with the minimum amount of mechanism.
Neither PEP-315 nor any of the "improvements" I've seen do that.

John Roth
Jul 18 '05 #3
Wayne Folta <wf****@netmail.to> wrote in message news:<ma*************************************@pyth on.org>...
2. It is a very general construct, which might be what is called for.
But I wonder if most of the time it would be used to accomplish
something like:

while 1:
line = sys.stdin.readline()
if line == "\n":
break


If you're concerned about the "beauty" of such code, then the
appropriate way to rewrite it is with a custom iterator/generator.

def untilBlankLine(fh):
line = fh.readline()
while line != '\n':
yield line
line = fh.readline()

And then you can use it like so:

for line in untilBlankLine(sys.stdin):
doSomething(line)

And remember, you can also create custom iterators with the builtin
iter:

for line in iter(sys.stdin.next, '\n'):
doSomething(line)

So I really see no need for any more complex looping construct. The
further we get from encouraging the use of while instead of for, the
better.

Jeremy
Jul 18 '05 #4
John Roth <ne********@jhrothjr.com> wrote:
1. The proposed syntax breaks Python's indentation scheme in a manner that
try-except, for-else, while-else, and if-elif-else do not.
I don't know about 'breaks', but IMO the main disadvantage of the 315
proposal is that you have to read code backwards. That is, given:

# Comment
#
while condition:
loop_body

you have to scan backwards to see whether the while block is stand-alone or
part of a do...while construct.

This could be solved by starting with a 'while' construct as normal and then
following it with the new keyword:

while True:
setup_condition
anwhile condition:
loop_body

('anwhile' is by analogy with 'elif', but anything could be used.)

This also has the advantage that more than one anwhile clause could be used,
giving a general-purpose multi-test loop.

Wayne's 'until' proposal is simpler than 315 and only allows a post-test,
not a mid-test loop.

IMO the word 'until' is not a good choice as it inverts the condition logic:
now the condition must be False to continue the loop instead of True as with
'while' and 'if'. If the difference is just that the loop executes at least
once without checking the condition, we could do it with a variant of while
syntax, for example:

while condition, 1:
loop_body

(PXTL has something like this: px:while elements may have a 'min' attribute
giving a minimum number of untested iterations. Seems to work OK, though I
have never had cause to use a number other than 1.)
2. What I find more serious is that there are a huge number of ways to go in
"improving" the basic while loop, none of which seem to be a natural
stopping point.


Indeed. I would quite like to use 'anwhile', but only *quite* - It's a *bit*
nicer than "if not condition: break", but not enormously so.

At best, I'm +0 on loop enhancements in general.

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/
Jul 18 '05 #5
> This could be solved by starting with a 'while' construct as normal and
then
following it with the new keyword:

while True:
setup_condition
anwhile condition:
loop_body

('anwhile' is by analogy with 'elif', but anything could be used.)


Hmmm... You could spell it "and while" without needing a new keyword at all.
Moreover, if you were to define "while:" as equivalent to "while True:",
then you could do this:

while:
setup_condition
and while condition:
loop_body

No reading backwards needed.
Jul 18 '05 #6
"John Roth" <ne********@jhrothjr.com> writes:
2. It is a very general construct, which might be what is called for. But I
wonder if most of the time it would be used to accomplish something like:

while 1:
line = sys.stdin.readline()
if line == "\n":
break

In a neater way? How about, instead, creating an "until" loop:

until line == "\n":
line = sys.stdin.readline()


Most other languages have a way to put the test at the bottom of the loop.
Pythonically that could look like:

repeat:
line = sys.stdin.readline()
until: line == '\n'

That would have similar indentation characteristics to try...finally.
Jul 18 '05 #7
Wayne Folta wrote:
1. It's clever, addresses a definite "wart", and is syntactically
similar to try/except. But it's syntax seems like an acquired taste to
me.
It's also analogous to the way do...while structures are written in
other languages, as well, so it lends familiarity by import.

I personally support the PEP, particularly with its generality.
In a neater way? How about, instead, creating an "until" loop:

until line == "\n":
line = sys.stdin.readline() [inserted indentation manually, didn't come through here]
Would be defined to work exactly like a while loop except the test is
not evaluated the first time through the loop. Wouldn't this be akin
to checking at the end of the loop, while maintaining a more while-ish
syntax?
The problem is it doesn't read like it runs. The suite is executed
_first_, then the test is repeatedly done and the suite is executed if
it is correct, like a while loop. In some sense you have some "voodoo
magic" here because code is not executed in the order you write it.
It's much better when it's written in the order it is executed, at least
as much as is reasonable. The do...while form does that well.

Also, `until' isn't the right word here. "Until x, do y" just means to
repeatedly do y until x becomes true. In analogue with "while x, do y,"
at first glance all it seems is that the `until' construct does the
logical negation of the conditional expression, but nothing else; that
is, it would seem logical to conclude that

until condition:
doSomething

is precisely the same as

while not condition:
doSomething

which is not at all what you mean. This form of `until' is indeed
included in other languages like Perl. (That doesn't mean that Perl's
way is the right way, it's just that this is an area which would be ripe
for confusion.)

You could use some keyword other than `until', but I think the confusion
of ordering is still there. Something gets executed, then a while loop
occurs, the best way to do that is to have that something appear above
the while loop, and that's just what the do...while form accomplishes.
Is this at all useful or is something of the order of PEP-315 the way
to go?


I think the PEP 315 method is superior.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Sometimes life gets in the way / You'll survive, be strongest
-- Des'ree
Jul 18 '05 #8
> Most other languages have a way to put the test at the bottom of the loop.
Pythonically that could look like:

repeat:
line = sys.stdin.readline()
until: line == '\n'

That would have similar indentation characteristics to try...finally.


I believe it would be more useful to allow the test to be anywhere in the
loop, not just at the beginning or at the end. Even better would be to
allow more than one test.
Jul 18 '05 #9
"Andrew Koenig" <ar*@acm.org> writes:
I believe it would be more useful to allow the test to be anywhere in the
loop, not just at the beginning or at the end. Even better would be to
allow more than one test.


It's already possible to test anywhere in a loop, with "if ... : break".
Jul 18 '05 #10
>>>>> "Josiah" == Josiah Carlson <jc******@nospam.uci.edu> writes:

Josiah> Personally, I find altering the syntax of while, or adding
Josiah> the 'do' to be a wart. I've never had problems
Josiah> understanding or writing code like the following; while 1:
Josiah> #setup if <condition>: break #body

Josiah> If I had a vote, I'd be -1 on PEP 315.

Amen to that! I think this is only a matter of education. People
should be told that 'while 1' construct is a good, simple, pythonic
way to do the thing. They are often prejudiced, thinking that there is
something 'wrong' with the construct. Hearing that it's ok to do
things this way can actually be very liberating: there is no reason to
feel guilty, and they will quite soon learn to appreciate how natural
and versatile the construct is.

Naming it 'loop' would drive the point even further, of course.

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #11
> Amen to that! I think this is only a matter of education. People
should be told that 'while 1' construct is a good, simple, pythonic
way to do the thing. They are often prejudiced, thinking that there is
something 'wrong' with the construct. Hearing that it's ok to do
things this way can actually be very liberating: there is no reason to
feel guilty, and they will quite soon learn to appreciate how natural
and versatile the construct is.

Naming it 'loop' would drive the point even further, of course.


The nice thing about while is that it is general. You can put anything
that returns some value that can be tested for truthfulness, even 1 (as
we like to do). Changing the name alters the linguistic interpretation
of what goes on. Using "loop" would suggest a subsequent "until":

loop:
#setup
#loop internals
until <failure condition> | <not successful condition>

Which is clearer, but adds two keywords. Though, like I said before,
the while setup is perfectly understandable.

- Josiah
Jul 18 '05 #12

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

Similar topics

3
by: Christoph Becker-Freyseng | last post by:
Hello, recently there was a lot discussion about a new Path-class. So Gerrit Holl started to write a pre-PEP http://people.nl.linux.org/~gerrit/creaties/path/pep-xxxx.html We tried to...
45
by: Edward K. Ream | last post by:
Hello all, First of all, my present state of mind re pep 318 is one of sheepish confusion. I suspect pep 318 will not affect Leo significantly, but I am most surprised that an apparently...
27
by: John Roth | last post by:
PEP 263 is marked finished in the PEP index, however I haven't seen the specified Phase 2 in the list of changes for 2.4 which is when I expected it. Did phase 2 get cancelled, or is it just not...
0
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
8
by: Micah Elliott | last post by:
I also have this living as a wiki <http://tracos.org/codetag/wiki/Pep> if people would like to add comments there. I might try to capture there feedback from this group anyway. First try at a PEP...
2
by: Bryan Olson | last post by:
Though I tried to submit a (pre-) PEP in the proper form through the proper channels, it has disappeared into the ether. In building a class that supports Python's slicing interface, ...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
4
by: dustin | last post by:
I've been hacking away on this PEP for a while, and there has been some related discussion on python-dev that went into the PEP: ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.