Debugging (was Re: Why not allow empty code blocks?)

Terry Reedy tjreedy at udel.edu
Sun Jul 31 14:58:06 EDT 2016


On 7/31/2016 6:18 AM, BartC wrote:

> The costs are near zero: at minimum, a syntactic construct such as:
>
>  repeat N:
>
> that expands to:
>
>  for _ in range(N):
>
> The benefit is not so much performance, but being able to express
> something very easily and quickly.

The cost of the 'repeat' contraction is that one cannot use the loop 
variable, either as part of a modified computation or for monitoring or 
debugging purposes.
        print(i, for_body_result)
Beginners are often atrocious at debugging, and it seems not to be 
taught hardly at all.  'repeat n' erects a barrier to debugging.

Debugging: probing a computation to see what actually happens, as 
opposed to what one wanted and expected to happen.  (Me, just now ;-)

One major way of debugging is printing values as they are computed. 
Naming values (objects) allows them to be printed without recomputing 
the value.  In the 'repeat n' context, recomputing would mean adding 3 
lines of debugging code instead of 1.

i = 0
repeat n:
    a = f(a)
    print(i, a)
    n += 1

As for the original topic: Guido judged that a uniform rule "Compound 
statement headers end with ':' and the next line has an additional 
indent" would make correct code easier to write and parse and make it 
visually more obvious.  Some Python aware editors like IDLE 
automatically add the indent.

-- 
Terry Jan Reedy




More information about the Python-list mailing list