do...until wisdom needed...

Steve Lamb grey at despair.rpglink.com
Mon Apr 16 11:47:17 EDT 2001


On Mon, 16 Apr 2001 07:54:12 -0700, Ken Peek <Peek at LVCM.comNOSPAM> wrote:
>The do/until construct is *still* needed IMHO, to allow for a more structured
>approach to programming.  

    Operative word, opinion.  Personally, I quite like not having a gaggle of
loop constructs to go through.  2 suits me just fine.  One that loops over a
block of code until I tell it to break for some reason or another and one that
loops over a list until the list is finished or I tell it to break for one
reason or another.  That covers darn near all cases that I can think of
without getting into special cases and trying to remember symantics of one
versus another.  Granted, iterating over a list could be done within the
confines of the other loop but it is something done often enough that I agree
with Alex in that it deserves its own syntax.

    Everything else, no.

>Many folks have given excellent suggestions on how I could FORCE the "while"
>construct to do what I want, 

    Then you question was answered.

>Now, if you please,-- Just make believe that Guido announced that he was
>going to put a "do/until" construct into the next release, and wants your
>opinion of how the syntax should work.  You have no choice-- it IS going into
>the language.

    This is how it would be done:

while (1):
    do.stuff()
    if condition:
      break

    You wanted to know the Python way, that /IS/ the Python way.  It may be
idiomatic, it may piss you off, it may offend your sensibilities, but to me it
is far easier to read than:

do:
  do.stuff()
until condition

    Why?  Because I have to remember what the difference is between do and
while (and C/Perl's for since I'm sure you're in love with that construct as
well).  Of course let's not even get into mentioning that in neither case does
the keyword actually tell you anything about the loop itself as both can
contain escape or shortcut conditions.

    To give an example of why more keywords are a bad thing, look at Perl.  I
have to because that is my job as much as I wish I could work in Python.  But
look at Perl and let's take something even far simpler than a loop.  Let's
take a simple conditional statement.

print("I'm a packet!") unless (packet_loss);

    Simple enough.  Readable enough right?  Well, what happens if you wanted
to put an else?  Well, now you have to change it around, right?

unless (packet_loss) {
  print("I'm a packet!");
}
else {
  print("I'm lost!");
}

    Whoops, problem, no else on unless.  So now we have to change it around.

if (!packet_loss) {
  print("I'm a packet!");
else {
  print("I'm lost!");
}

    Well, lookie here, we've gone from a "readable" line to, well, a block
that most likely looks like a dozen other blocks of code.  What that means is
the first syntax I gave is a specialized case that needs to be learned in
addition to the mainstay case.  In perl there are 4 ways to express the above
simple example, 4 different things that need to be learned.  Python, 1.  Guess
which one many people consider easier to learn, read and write.  

    So, you want to know what the Python way would be?  You were told.  You
want to reimplement the language for your embedded system and include a
do..until loop?  Fine.  But remember that is /your/ language, the final
decision rests with you, and chances are it won't be Pythonic if you implement
do..until.

-- 
         Steve C. Lamb         | I'm your priest, I'm your shrink, I'm your
         ICQ: 5107343          | main connection to the switchboard of souls.
-------------------------------+---------------------------------------------



More information about the Python-list mailing list