The "loop and a half"

Paul Moore p.f.moore at gmail.com
Wed Oct 4 11:56:16 EDT 2017


On 4 October 2017 at 16:35, Steve D'Aprano <steve+python at pearwood.info> wrote:
> I've been programming in Python for twenty years, and I don't think I have
> ever once read from a file using a while loop.

Twenty years isn't long enough :-) The pattern the OP is talking about
was common in "Jackson Structured Programming" from back in the 1980s.
At the time (from what I recall, it *was* 35+ years ago after all!),
it was a reasonably elegant way of handling reading of data that's
structured as

    header
      subheader
        body
        body
      subheader
        body
        body

while maintaining per-section style subtotals (I believe this is
referred to as a "break report" in certain business contexts).

Nowadays, I wouldn't code such a report like that. Like you say, I'd
use a custom iterator or something similar. Although getting the
details and edge cases right of such a pattern is still a surprisingly
tricky problem. So I guess it's a good "learning to program" exercise,
but the "while loop with break part way through" pattern for solving
it is not really what I'd want to see taught to beginning Python
programmers...

Paul



More information about the Python-list mailing list