[Python-ideas] Simpler syntax for basic iterations

Terry Reedy tjreedy at udel.edu
Mon Oct 12 08:10:24 CEST 2015


On 10/11/2015 5:47 PM, Luciano Ramalho wrote:
> I've felt the same need Andre described, and so far I have not seen a
> convincing argument against his proposal to accept this syntax:

Your logic is backwards.  The default answer to 'add this' is 'No'.  The 
burden of persuasion is on you.  The people who have to be persuaded, by 
their own standards of goodness for Python, are Guido and core developers

>
> for n:  # repeat block n times
>       fd(100)
>       lt(360/n)
>
> Where n is any expression that evaluates to an integer.

Andre wanted, I believe, 'for <literal number>:' to avoid naming 
numbers, or at least variable quantities.  But I am 'not convinced'. 
The above should be

turn = 360/n
for n:
   fd(100)
   lt(turn)

Anyway, very young kids get the concept of 'variable quantity': number 
of days left to Christmas, number of cookies handed out, etc.

> The goal is to repeat some operation N times without creating a
> spurious variable.

The fraction of for loops using range(n) is small, especially after 
enumerate was introduced, and especially if n is a constant.  The 
fraction of such loops where the iteration count can never have any use 
is also small, because if the code has a bug, you will likely want the 
iteration count.  Adding 'print(i, a, k)' to a loop, where a and k 
change with each loop is, for me, a very common debugging tool.

Even without bugs, I believe that displaying iteration counts can help 
learning and program understanding.  Right now, one could step through

n = 17
for i in range(n):  # i = number of chord already drawn
   fd(100)
   lt(360/n)

with IDLE's debugger and see the step number increase just before a 
chord is drawn.  Or one might want to add 'label(i)' at the top, where 
label(i) puts a small circled number near the current position. The more 
I think about this, the less I am convinced that the fraction of truly, 
permanently spurious loop counts is enough to support a new syntax.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list