Looping constructs

Peter Hansen peter at engcorp.com
Mon Dec 24 14:33:33 EST 2001


beej at piratehaven.org wrote:
> 
> In article <3C2602B3.47EA5685 at engcorp.com>,
> Peter Hansen  <peter at engcorp.com> wrote:
> >I, for one, find it much easier to start my loop
> >with "while 1:" and then stick the logic where it needs
> >to be with "if xxxx: break"
> 
> Do you ever have a "while 0:" loop?  Of course not.  

You'd be surprised. :-)  I periodically have "if 0" and 
"while 0".  But only when I'm disabling code during debugging...
(yes, not to the point :)

> So why is there even a conditional in there at all?  

I'm sorry I was too succinct.  Of course I use the 
conditional case from time to time.  Just not often.  Rarely
enough that I don't feel I need anything more complicated.  
Rarely enough that when a situation arises which could use 
some other kind of looping, with the test at the bottom
of the loop and inverted, for example, I tend simply
to revert to the "while 1" case and make things clean and 
clear.

> Shouldn't Python just have a:
> 
> foreverloop:  # do-while, repeat-until
>         do_stuff()
>         if condition: break
> 
> foreverloop:  # while
>         if condition: break
>         do_stuff()
> 
> No, because that's ugly.  (Or maybe yes because it's minimalistic?)

It already has that, and it's spelled "while 1".  Yes,
it's a little ugly.  Yes, it's minimalist.  Yes, there
are glossier alternatives, and no, in my opinion we don't
really need them.  And since we already have "while 1"
and we don't have "foreverloop" and there is no practical
gain in adding it, we'll likely stick with what we have.

> I'm one of those Python newcomers who thinks the set of looping
> statements is a little too rarified. :)  But whatever, I'll keep using
> it.

My own thoughts at first...  Once you start to trust
Guido's judgment in matters of language design, you may find 
(a) you rationalize some of the ugliness, and (b) you suddenly
have more time for writing working, maintainable code and less
time spent on discussions of language design. <wink>

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list