[Python-ideas] More classical for-loop

Mikhail V mikhailwas at gmail.com
Thu Feb 16 22:13:19 EST 2017


Here is a summary of my idea about for-loop.
It focuses on readability and does not
take in account possible technical nuances.
This is my first attempt to write a full proposal
and I suppose it is ok to post it here.
Many things (readability) can raise opinion based dispute,
so it is sort of my opinion.
I am not sure how topical/realistic is it,
or contains technical flaws, so your note and
recommendations are welcome.


Proposal
--------

Introduce a more classical, "Fortran-ish" loop syntax.
The simplest syntax could be:

for i somekeyword 10 :

which logically will be the same as

for i in range(10) :

Keyword supposed to have sense only inside the statement,
thus not interfere with variables and other keywords.
At this moment the proposal will be to use the word "over":

for i over 10 :

The choice of the word is based only on its look - middle
sized, has related meaning(?), and easy to type. Of course,
if a better word can be found, it can be used.

Note: the meaning of the word is of much less
importance than optical qualities.

Other good looking options:

for i count 10 :
for i range 10 :
for i steps 10 :
for i sieve 10 :

Parameters same as in range():

for i over  a, b, step :

is the same as

for i in range(a, b, step) :


Rationale
-----------

Removing the brackets will help concentrate on other
parts of code, though it has some price, since if more
than one range parameter is used, it loses some emphasis.
This is easiliy compensated by IDE's syntax highlighting,
namely by slightly graying out the keyword.

Currently the loop statement uses the word "in".
This word creates rather unpleasant visual 'stick and hole'
effect on eyes due to its short size. Is quite noticable,
especially in case of one-letter varaibles (e.g. for i in ...).
And the 'bulky' range() part is bit too heavy.
More problems to 'holes' effect, especially in
monospaced editors, adds the form of glyphs "i" and "n"
which both have vertical stick structure and also interfere
with the very common variable name "i".

Also new sytax needs less typing.


Examples
--------

Common use case:

L = [1,3,5,7]

for i over len(L):
   e = L[i]

or:

length = len(L)
for i over length:
   e = L[i]

Nested:

for y over  0,480 :
   for x over  0,640 :
      putpixel (x, y)

or:

width = 640
height = 480
for y over  0, height :
   for x over  0, width :
      putpixel (x, y)

Surprisingly good result is achieved if simply
remove the keyword and put more space:

for y    0, height :
   for x    0, width :
      putpixel (x, y)

But similar effect can be achieved by
graying out the word with syntax highlighting.

Compare with existing syntax:

for y in range(0,480) :
   for x in range(0,640) :
      putpixel (x, y)

for y in range(0, height) :
   for x in range(0, width) :
      putpixel (x, y)


Notes
-------

Although it is true that the whole idea is more
about cosmetics, still the  for-loop is important
structural component and slight improvement
could count.


Mikhail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170217/a70b736c/attachment-0001.html>


More information about the Python-ideas mailing list