[Python-ideas] "while:" for the loop

Tobias Kohn kohnt at tobiaskohn.ch
Wed Sep 26 09:42:16 EDT 2018


  Hello,

Although I doubt it will really make it into Python's grammar, I am  
all +1 for the idea of having "repeat" as a loop keyword in Python.   
Actually, I have been using "repeat" as a keyword in Python for quite  
some time now, and found it not only convenient, but also a great help  
in education.

My version of "repeat" has two versions.  The primary usage is with an  
expression that evaluates to a number and specifies how many times the  
loop is to be repeated:
```
repeat <INT>:
    <SUITE>
```
The second version is without the number, and, indeed, stands for an  
infinite loop.
```
repeat:
    <SUITE>
```
I must admit, though, that I had hardly ever reason to use the second form.

The implementation is currently built on top of Jython, and uses  
something like a preprocessor to implement the "repeat"-keyword.  In  
order to keep some backward compatibility, the preprocessor does check  
a few cases to determine how it is used, and does not always treat it  
as a keyword.

Even though it is possible to write a parser that detects if "repeat"  
is used as the keyword for a loop or anything else, it will make the  
syntax very brittle.  Take, for instance:
```
repeat (3*4)
```
Now, is this supposed to be a function call, or is it a  
`repeat`-statement with a missing colon at the end? 

I therefore would strongly advise against having a word act as a  
keyword sometimes, and sometimes not.  Probably a better solution  
would be to have something like `from __features__ import repeat` at  
the beginning.

My reason for introducing "repeat" into Python in the first place was  
because of didactical considerations.  Even though it might not seem  
so, variables are indeed a very hard concept in programming,  
particularly for younger students.  And variables get particularly  
hard when combined with loops, where the value of a variable changes  
all the time (this requires a lot of abstract thinking to be properly  
understood).  Loops alone, on the other hand, are a relatively easy  
concept that could be used early one.  So, there is this dilemma: how  
do you teach loops at an early stage without using variables?  That's  
when I added the "repeat"-keyword for loops, and it has worked  
marvellously so far :).

Cheers,
Tobias

Quoting James Lu <jamtlu at gmail.com>:

> repeat could be only considered a keyword when it’s used as a loop
>
> Sent from my iPhone
>
>> On Sep 26, 2018, at 8:46 AM, Brice Parent <contact at brice.xyz> wrote:
>>
>>> Le 26/09/2018 à 14:33, James Lu a écrit :
>>> what about “repeat:”?
>>>
>>> Sent from my iPhone
>>
>> I'm not sure it was on purpose, but you replied to me only, and not  
>> the entire list.
>> I believe the adding of a new keyword to do something that is  
>> already straightforward (`while True:`), and that doesn't add any  
>> new functionality, won't probably ever be accepted.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideasCode of  
> Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180926/213aaa20/attachment-0001.html>


More information about the Python-ideas mailing list