"do" as a keyword

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Dec 12 19:09:16 EST 2007


On Wed, 12 Dec 2007 09:46:09 -0600, Chris Mellon wrote:

> On Dec 11, 2007 2:19 PM, Steven D'Aprano
> <steve at remove-this-cybersource.com.au> wrote:
>> 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



More information about the Python-list mailing list