do...until wisdom needed...

Andrew Dalke dalke at acm.org
Mon Apr 16 13:43:10 EDT 2001


Ken Peek wrote:
>I am writing my own language based on Python for deeply embedded systems.
I
>am not trying to change the Python language.  I would like all of the
>traditional looping constructs in my new language, and so was trying to get
>your opinion as to the best way to implement a "do...until" construct
  ...
>So, now that you know that I really am asking what I said I was asking,
>how about answering my original question?

Your original question:
> If Python WERE to have a "do...until" construct, what
> would be the best way to implement the syntactical rules?

These two questions aren't the same.  Your original one refers to
Python, which means amoung other things a viewpoint that a minimal
number of contructs - but not the absolute minimum because there
are practical reasons for non-orthogonality - is appropriate.

In my C code (I've written order(100KLOCs) of production code)
I use "do" about once a year.  Every time I use it I need to pull
out K&R or grep existing code because I've forgotten the syntax.
The pythonic view is that this construct saves at most two lines
which does not justify the effort of having to remember how
a do..until is used.

BTW, in my C code I now use while(1) { code; if (cond) break;}

I pulled out K&R 2nd edition, section 3.6 pp63-64 talks about
C's Do-while loop.  It says:

] Experience shows that do-while is much less used than while
] and for.  Nonetheless, from time to time it is valuable, as
] in the following function itoa ...

The atoi code could be rewritten to use a while with overall
two more lines of code.  If you agree that do..while is used
5% of the time while is used, and while construct syntax takes
up less than 1% of working code, then there is a 0.1% increase
in code size at the expense of understanding.

That's one fundamental reason why do..until is not Pythonic.

The other one is that no one could think of a nice way to
do the indentation given that it requires two control statements,
and all control statements in Python are dedented, end with a :
and have code following the statement.  The "until" clause
breaks one of those, unless you always follow it with a dummy
"pass" statement.

Now to your real question:
> I would like all of the traditional looping constructs in
> my new language, and so was trying to get your opinion as to
> the best way to implement a "do...until" construct.

By all means, use

do:
  code
until condition

That's not Pythonic and won't be added to Python, but you
aren't talking Python.  Although I would suggest using 'while'
because of the parallel with C and because it saves a keyword.

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list