Friday Finking: Contorted loops

Terry Reedy tjreedy at udel.edu
Thu Sep 9 19:47:10 EDT 2021


On 9/9/2021 5:36 PM, dn via Python-list wrote:
> Why does Python not have a repeat-until loop construct?

1. It is not needed.  You covered that.

2. It is rare useful.  For loops are common.  While loops are occasional 
(nearly an order of magnitude less common than for loops.  Fractional 
loop constructs are rare.  ("loop-and-a-half" is a mislabel since at not 
even one loop is guaranteed.)  "do-while" or "repeat-until is even rarer 
since fractional-loop include this as a special case.

3. Adding 'until' as a keyword *now*, rather than in 1.0 or at least 
several versions ago, has cost so far judged to outweigh the small 
benefit.  The PEP parser makes contextual keywords much more easily 
possible but there is a cost to having a context dependent grammar. 
Consider this 3.10.0 snippet:

 >>> match, case = 1, 1
 >>> match match:
...     case case:
...         print('matched')
...
...
matched
 >>> match case:
...     case match:
...         print('matched')
...
...
matched

To me, having a word sometimes be a keyword and sometime not make code 
harder to read.  In IDLE, it is a bit easier as the keyword uses of 
'match' and 'case' above are correctly highlighted as keywords, and the 
non-keywords uses not highlighted.  But this is harder that for 
full-time keywords with sane code that works in an re-based highlighter.

Underscore, not used above, but also a new contextual keyword, is even 
harder.  Three of us could not get all the cases we tested correct and I 
suspect doing so without running the PEG parser may be impossible. 
Since highlighting is redone with each keystroke, I suspect doing the 
latter would add a noticeable and unacceptable lag between keystrokes 
and display.



-- 
Terry Jan Reedy



More information about the Python-list mailing list