while true: !!!

Greg Jorgensen gregj at pobox.com
Tue Dec 19 01:38:27 EST 2000


"Blair Lowe" <Blair.Lowe at compeng.net> wrote:

> This is exactly what a python program should be NOT doing. "while 1"
> is one of the most cryptic statements in Computer Science.

That's a bit of an exaggeration. I thought we decided that Knuth's MIX
renditions of common algorithms were the most cryptic statements in Computer
Science.

> other languages are known for this stuff, but I do not know why python
> doesn't auto define true and false, so that we don't have to write
> unreadable stuff.

Adding a new boolean type to Python would add needless complexity to solve a
non-problem. Even a simple new type that consisted of only true and false
would require conversion rules that would affect every existing Python
library.

You can define true and false yourself, as many C/C++ header files do, but
then you run the risk of stuff like this:

    true = 1
    false = 0
    ...
    a = len(s)
    if a == true:  # fails unless a is 1

It's much harder to track this kind of thing down than learning once in your
career what "while 1" means.

> While "while 1" (pardon the pun) is a standard fixture that anyone
> with any experience knows what is going on, the whole point is to
> make all our code readable so that we, and others can maintain it and
> reuse it easily.

Exactly--that's why learning idioms is as important as learning syntax.

> Although the definition is cryptic, the resulting code is
> very readable. That is what we want.

Until it doesn't work. Encountering "while true:" in a program, a newbie may
assume that true is a Python keyword. I remember a C++ programmer I worked
with who though that true and false were keywords until he discovered this
(typical) definition in a header file:

    enum { false = 0, true = -1 };

--
Greg Jorgensen
Deschooling Society
Portland, Oregon, USA
gregj at pobox.com





More information about the Python-list mailing list