The rap against "while True:" loops

Mensanator mensanator at
Wed Oct 14 03:26:17 CEST 2009

On Oct 13, 5:38�pm, "Rhodri James" <rho... at>
> On Tue, 13 Oct 2009 22:59:04 +0100, Mensanator <mensana... at> wrote:
> > And I'm not saying John nor the OP should stop
> > using what works for them. But there are certainly
> > valid reasons for "don't use while True" to be
> > on the "Best Practices" list.
> Unfortunately, some of them seem to be reasons from
> my point of view to put "*do* use while True" on the
> "Best Practices" list. �

Really? Which ones?

> Some of the constructs you
> seem to like ring big alarm bells with me, because
> I've found entirely too many bugs hidden by them.

For example?

> > After all, how many times hve you put 'break'
> > in a loop comprehension?
> How many times have you written 20-line list
> comprehensions? �

You mean something like this?

p = [''.join
for c0 in a for c1 in a for c2 in a for c3 in a for c4 in a for c5 in
a for c6 in a for c7 in a for c8 in a for c9 in a for c10 in a for c11
in a for c12 in a for c13 in a for c14 in a for c15 in a for c16 in a
for c17 in a for c18 in a for c19 in a if (c1>c0) and (c2>c1) and
(c3>c2) and (c4>c3) and (c5>c4) and (c6>c5) and (c7>c6) and (c8>c7)
and (c9>c8) and (c10>c9) and (c11>c10) and (c12>c11) and (c13>c12) and
(c14>c13) and (c15>c14) and (c16>c15) and (c17>c16) and (c18>c17) and

Actually, I didn't write it, the computer did:

    v = ','.join(['c%s' % i for i in r0])
    f = ' '.join(['for c%s in a' % i for i in r0])
    i = ' and '.join(['(c%s>c%s)' % (j,j-1) for j in r1])
    e = ''.join(["p = [''.join((",v,")) ",f," if ",i,"]"])

> If the answer is more than zero,
> what on earth possessed you to think it was a
> good idea? :-)

I didn't. It was a feasibility study. But that was
in the days before itertools was upgraded to handle
all the subsets of the Cartesian Product. No need for
such silliness now. And itertools works better.

> --
> Rhodri James *-* Wildebeest Herder to the Masses

More information about the Python-list mailing list