[Python-ideas] Updating PEP 315: do-while loops

Larry Hastings larry at hastings.org
Sun Apr 26 22:20:25 CEST 2009


Mike Meyer wrote:
> This is just arguing about the meaning of "syntactic sugar". In some
> sense, almost all control constructs are syntactic sugar, as all you
> really need is lambda and if
> (http://en.wikipedia.org/wiki/Lambda_Papers). However, anything that
> lets me reduce the line count by removing boilerplate or - even
> better, duplicate code - moves out of that category for me. Please
> feel free to disagree.

Then by your own definition do/while has yet to move out of the category 
of "syntactic sugar".  do/while does not reduce line count, and if it 
could be said to remove boilerplate or duplicate code it does so to a 
vanishingly small degree.

So what's left?  My working definition of "syntactic sugar" is: 
"likely-redundant syntax that makes expressing a program more appealing 
somehow".  I think do/while is syntactic sugar.  Is it worth making the 
language bigger to add it?  Particularly when we can't agree on what 
would be good syntax?  The fact that we're still having the debate 
implies... well, that people like arguing on the internet I guess.  But 
do/while can be found in Pascal, and C / C++, and all the C clones 
(Java, C#) cloned that too, so perhaps it has merit.

Anyway, this is one reason why I like the "do:" statement I proposed.  
(The other one--the one where I also proposed "continue if" and "break 
if".  But given your reaction to those let's ignore 'em for now.)  This 
isn't *pure* sugar--it's a mildly novel flow control construct for 
structured programming.  Certainly Python doesn't have anything like it 
right now; "while True:" isn't an exact match (and "while False:" is 
equivalent to "if 0:").  My "do:" would allow spelling do/while as:

    do:
        something()
        if condition: continue

I think that's a reasonable do/while, and so economical: we only added 
one new keyword.  This form of "do:" has other uses too, as per my 
previous email.

Not that I expect this syntax to take the world by storm.

>>     do:
>>         something()
>>     while condition
>>     
> Except Guido has already rejected this - or at least stated that he doesn't really like it.

I can believe that.  I still think it's as good as we're going to do for 
a literal translation of do/while.

If there was a syntax everyone liked we'd already be using it.  The fact 
that we haven't hit on one by now--recall that the PEP is six years 
old--implies that we're not likely to find one.  We've added lots of 
syntax to Python over the last six years, stuff that has made the 
language more powerful, more flexible, and even faster in some places.  
do/while doesn't give us any new power, flexibility, or speed--I daresay 
if we never added do/while we wouldn't really miss it.

If you have it handy, can you cite where he said he didn't like that?  
Not that I don't believe you; I'd just like to read it.

Has Guido cited a syntax that he *did* like?


/larry/



More information about the Python-ideas mailing list