Looping [was Re: Python and the need for speed]

Gregory Ewing greg.ewing at canterbury.ac.nz
Sun Apr 16 19:30:59 EDT 2017


bartc wrote:

>  > - describing the various syntax forms;
>  > - explaining how they differ;
>  > - tutorials for beginners showing each form;
> 
> And you don't have to explain how an endless loop should be written as 
> 'while True', meanwhile advising against using 'while 1'?

You don't have to mention it in the reference docs, because it's
deducible from the rest of the language rules. Nor do you need
an explicit test for it, unless there is code in the compiler
specifically to optimise that case.

Tutorials might want to mention it, but it's not strictly
necessary, as a smart enough student could figure it out for
themselves.

> So, how many kinds of sequences do you have in Python?
> 
> lists
> tuples
> namedtuples
> arrays
> bytearrays
> string ?
> memoryview

By far the most commonly used ones are list and tuple. The
rest are designed for specialised uses, and have characteristics
that can't easily be replicated using the fundamental types,
if at all.

Alternative looping constructs, in contrast, have *no*
functional difference, their only advantage being that they
provide a slightly more succint way of expressing certain
things.

>   While a; b; c; d Do e; f; g End
> 
> with the test (the value of 'd') effectively in the middle.

That would be a rather obtuse way of expressing it, though.
It looks like the whole of 'a; b; c; d' is the condition,
whereas you're really only using a, b and c for their side
effects.

The whole point of a loop-and-a-half construct is to make
the condition stand out, despite being in the middle.

This is actually the only additional looping construct that
I wouldn't mind seeing. It gets brought up now and then;
last time, if I recall, the discussion fizzled out due to
a feeling that you don't really need it much in Python,
because those kinds of loops are usually better expressed
using an iterator.

> Most of my loops start off as endless loops, until I can determine the 
> actual terminating condition, and where it best goes.

Interesting. My experience is quite different. Most of
the loops I write start off with me thinking "Now I want
to do this for each element of this collection", which
maps straight to a Python for-loop.

In the rare cases where I do use a while loop, usually I
have a fairly good idea what the terminating condition is
going to be. Cases where a significant amount of head
scratching is needed to figure it out are pretty rare.
But maybe you're working on different kinds of problems
from me.

> Sometimes they stay as endless loops.

By design or by accident? :-)

Every program I've every written that had a deliberate
endless loop in it had exactly *one* of them, at the top
level. And I knew it would be that way before I started
writing it. (And since the only way to stop it would be
ctrl-C or equivalent, I only write such things if they're
throwaway tools for my own use.)

-- 
Greg


More information about the Python-list mailing list