472,988 Members | 2,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

"do" as a keyword

First off let me state that I really enjoy using Python. I am a 3rd
year student and have been using python for 3 months, (thanks to
trac!). I do not consider myself an experienced or clever programmer,
but I am able to get by.

Something I love about Python is that almost everything you do can be
written in pseudo code then carried across into Python without a great-
deal of difference.

But reading through the warts and reading about a lack of "do while
statements" I also started to ponder about the "'do something' if
'true' else 'do this'", and pondered if perhaps this statement could
do with the including of the keyword do.

It would clear up the usage of such a statement because it would read
happily. Or am I making a mountain out of an ant hill?
Dec 11 '07 #1
11 1608
co*********@gmail.com wrote:
First off let me state that I really enjoy using Python. I am a 3rd
year student and have been using python for 3 months, (thanks to
trac!). I do not consider myself an experienced or clever programmer,
but I am able to get by.

Something I love about Python is that almost everything you do can be
written in pseudo code then carried across into Python without a great-
deal of difference.

But reading through the warts and reading about a lack of "do while
statements" I also started to ponder about the "'do something' if
'true' else 'do this'", and pondered if perhaps this statement could
do with the including of the keyword do.

It would clear up the usage of such a statement because it would read
happily. Or am I making a mountain out of an ant hill?
I doubt this will gain any traction. Similar proposals have been made
before - e.g. http://www.python.org/dev/peps/pep-0315/

But they didn't

There are a few tricks you can use to emulate the desired behavior without
being to disruptive to the eye. E.g.
while True:
do_something()
if condition: break

Granted,

do
do_something()(
while condition

is a bit more pleasant - but not enough to change the language I'd say.

Diez
Dec 11 '07 #2
On 2007-12-11, co*********@gmail.com <co*********@gmail.comwrote:
First off let me state that I really enjoy using Python. I am a
3rd year student and have been using python for 3 months,
(thanks to trac!). I do not consider myself an experienced or
clever programmer, but I am able to get by.

Something I love about Python is that almost everything you do
can be written in pseudo code then carried across into Python
without a great- deal of difference.

But reading through the warts and reading about a lack of "do
while statements" I also started to ponder about the "'do
something' if 'true' else 'do this'", and pondered if perhaps
this statement could do with the including of the keyword do.

It would clear up the usage of such a statement because it
would read happily. Or am I making a mountain out of an ant
hill?
When I use languages that supply do-while or do-until looping
constructs I rarely need them.

I personally miss

for init; condition; next:

more than do-while, but the structured for loop doesn't fit in
Python since init and next would (probably) have to be statements
(or statement suites) rather than expressions. Hence, the cool
innovations of iterators and generators, which otherwise might
not have found a home in Python. I wonder what programming Python
was like before iterators sometimes.

However, did you have an specific need for a do-while construct?
Perhaps we could show you the alternatives.

--
Neil Cerutti
Dec 11 '07 #3
I also started to ponder about the "'do
something' if 'true' else 'do this'", and pondered if perhaps
this statement could do with the including of the keyword do.
Python has support for this in versions >= 2.5:
>>a = range(0, 5)
b = range(5, 8)
min(a) if sum(a) < sum(b) else min(b)
0

In prior versions, you can use the and/or trick:
>>(sum(a) < sum(b) and [min(a)] or [min(b)])[0]
0

-Tor Erik
Dec 11 '07 #4
On Tue, 11 Dec 2007 15:06:31 +0000, Neil Cerutti wrote:
When I use languages that supply do-while or do-until looping constructs
I rarely need them.
....
However, did you have an specific need for a do-while construct? Perhaps
we could show you the alternatives.
"Need" is a strong word. After all, all looping constructs could be done
with a conditional jump, so arguably we don't "need" while or for either.

But loops that run at least once is a basic element of algorithms.
Perhaps not as common as the zero or more times of the while loop, but
still fundamental. It is a shame it has to be faked using:

while True: # force the first iteration to always run
process
if condition: break

Ugly and misleading. But here to stay until Guido has an epiphany and
decides that a do...until loop is elegant enough that it is well worth
the addition of two new keywords. (I find the idea of do...while
unspeakable. The test is backwards.)

Oh well... when I'm Malevolent Dictator For Life, Guido will do as he's
told, and no mistake!!!

*wink*.

--
Steven.
Dec 11 '07 #5

"Steven D'Aprano" <st***@REMOVE-THIS-cybersource.com.auwrote in message
news:13*************@corp.supernews.com...
||
| But loops that run at least once is a basic element of algorithms.
| Perhaps not as common as the zero or more times of the while loop, but
| still fundamental. It is a shame it has to be faked using:
|
| while True: # force the first iteration to always run
| process
| if condition: break
|
| Ugly and misleading.

I disagree. Nothing is being faked. The generic loop is

while True:
pre_process
if condition: break
post_process

If there is no pre_process, abbreviate the first two lines as 'while
condition:'. If there is no post_process, some would like another
abbreviation. Understanable. But the use cases seem relatively few. And
anyway, a competant programmer must understand the generic loop and a
fraction form, which I believe is at least as common as the no post_process
case.

Terry Jan Reedy

Dec 12 '07 #6
On 2007-12-12, Terry Reedy <tj*****@udel.eduwrote:
>
"Steven D'Aprano" <st***@REMOVE-THIS-cybersource.com.auwrote in message
news:13*************@corp.supernews.com...
||
| But loops that run at least once is a basic element of algorithms.
| Perhaps not as common as the zero or more times of the while loop, but
| still fundamental. It is a shame it has to be faked using:
|
| while True: # force the first iteration to always run
| process
| if condition: break
|
| Ugly and misleading.

I disagree. Nothing is being faked. The generic loop is

while True:
pre_process
if condition: break
post_process

If there is no pre_process, abbreviate the first two lines as 'while
condition:'. If there is no post_process, some would like another
abbreviation. Understanable. But the use cases seem relatively few. And
anyway, a competant programmer must understand the generic loop and a
fraction form, which I believe is at least as common as the no post_process
case.
And this generic loop is faked. There is no notion in the language that
somehow connects this if statement to be a breaking condition for a
loop. If it would be a real syntatic construct in the language, it would
probably look more like:

do:
pre_process
until condition:
post_process

Personnaly I would have preferred to have this one generic loop
construct with no abbreviations instead of having the while abbreviation
that is to be combined with a break statement to fake the generic loop.

--
Antoon Pardon
Dec 12 '07 #7
I find that when teaching beginning programmers, they usually think in
"until" terms, and not "while" terms.
If really beginning, an overview of this whole idea of control structures
makes sense, such as this wikipedia article:
http://en.wikipedia.org/wiki/Control_flow

Then explain how Python is very minimalist in its approach, unlike
some languages, which try to provide all kinds of control structure
semantics, including multiple case loops (do... case... case...)
which Python famously does not natively have either.
they find the "while" logic to be unintuitive, and I often find myself
feeling the same way: crafting it with the until logic, and then reversing
it.
I wouldn't make "intuitive" the guiding light in all cases, as it's
often just code for "conditioned reflex" or "what we're used to."
Usually beginners outgrow their initial discomfort, like when
learning to drive stick instead of automatic or whatever.

Kirby
Dec 12 '07 #8
On Wed, 12 Dec 2007 09:46:09 -0600, Chris Mellon wrote:
On Dec 11, 2007 2:19 PM, Steven D'Aprano
<st***@remove-this-cybersource.com.auwrote:
>On Tue, 11 Dec 2007 15:06:31 +0000, Neil Cerutti wrote:
When I use languages that supply do-while or do-until looping
constructs I rarely need them.
...
However, did you have an specific need for a do-while construct?
Perhaps we could show you the alternatives.

"Need" is a strong word. After all, all looping constructs could be
done with a conditional jump, so arguably we don't "need" while or for
either.

But loops that run at least once is a basic element of algorithms.
Perhaps not as common as the zero or more times of the while loop, but
still fundamental. It is a shame it has to be faked using:

I agree that it's fundamental, but I'd like to mention that I've written
many thousands of lines of Python code, from throwaway code for
demonstration to enterprisey servers and all sorts of things in between
and I've *never* written a "1 or more times" loop except when I was
demonstrating that exact thing. One thing that Python has definitely
changed my thinking about is that I tend to formulate both problems and
solutions in terms of iteration over sequence rather than as traditional
conditional based looping. If I need a "1 or more" loop I formulate the
problem as a sequence of 1 or more elements.
I'm not entirely sure this is a good, or bad, thing. Iteration over a
sequence is one mental tool. while and do loops are two others. Having
more tools available frees you to a wider range of possible solutions --
which can mean people can find better solutions, or they can find worse
solutions. One good tool is better in many ways than two poor tools, but
on the flip side, relying on only one tool constrains your thinking.

It's a shame that generators and iterators came to Python so late in its
history, otherwise Guido could have made the language even more
minimalist by using only one loop construct (for) and not needing the
while keyword.

*half-wink*
--
Steven
Dec 13 '07 #9

On Wed, 12 Dec 2007 09:46:09 -0600, Chris Mellon wrote:
I agree that it's fundamental, but I'd like to mention that I've
written
many thousands of lines of Python code, from throwaway code for
demonstration to enterprisey servers and all sorts of things in
between
and I've *never* written a "1 or more times" loop except when I was
demonstrating that exact thing. One thing that Python has definitely
changed my thinking about is that I tend to formulate both problems
and
solutions in terms of iteration over sequence rather than as
traditional
conditional based looping. If I need a "1 or more" loop I formulate
the
problem as a sequence of 1 or more elements.
How would you code an integrator-loop to stop when its error goes below
some limit with iterations? While loops are as far as I can tell
essential for
a number of problems in math and physics.

Cheers
TG
Dec 13 '07 #10
On Dec 11, 2007 4:06 PM, Neil Cerutti <ho*****@yahoo.comwrote:
>
However, did you have an specific need for a do-while construct?
Perhaps we could show you the alternatives.
I have wanted do-while loops in exactly one kind of algorithms, when
you generate something and you have to keep trying until it gets
right. For example, generating random and non-overlapping points on a
grid:

import random
grid = dict()
for i in range(10):
while True:
x = random.randint(0, 4)
y = random.randint(0, 4)
if not (x, y) in grid:
break
grid[x, y] = 1

The loop runs one or more times until a vacant spot in the grid is
found. This type problem would be better expressed using a do-while:

import random
grid = dict()
for i in range(10):
do:
x = random.randint(0, 4)
y = random.randint(0, 4)
while (x, y) in grid
grid[x, y] = 1
--
mvh Björn
Dec 13 '07 #11
But reading through the warts and reading about a lack of "do while
statements" I also started to ponder about the "'do something' if
'true' else 'do this'", and pondered if perhaps this statement could
do with the including of the keyword do.
I often miss what can be done in other languages
that support what I call 'side effect' assignment:

while x = get_val_somewhere():
process(x)

Although, I know that if I write the get function,
I could make it an iterator, and do:

for x in get_val_somewhere():
process(x)

in which case, I'm even happier.
--
Posted via a free Usenet account from http://www.teranews.com

Dec 14 '07 #12

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

Similar topics

11
by: Wayne Folta | last post by:
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...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
11
by: Brett | last post by:
What is the C# equivalent for this VB.NET code: Private WithEvents IE_Inst As New SHDocVw.InternetExplorer I get as far as: private SHDocVw.InternetExplorer IE_Inst = new...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
3
by: niraj.kumar.ait | last post by:
Hi, I have a variable "do" declared in the html form . When I go to assign do some value like below it gives error(since do is a keyword) .Is there any way to assign the value to "do" variable....
4
by: Yarco | last post by:
In c++ we could modify the private variable in a class use friend keyword. How could it be done in php? class YourBag { int money; friend class Thief; // friend? };
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.