while true: !!!

Bjorn Pettersen pbjorn at uswest.net
Mon Dec 11 12:30:21 EST 2000


Alex Martelli wrote:

> [snip]
> Anyway, in his landmark 1974 article on the Communications of
> the ACM, "Structured Programming WITH Go-To Statements", Dr.
> Knuth made the point that the generally desired form of a loop
> statement is (in pseudo-language):
>
>     loop
>         statements for part 1
>         exit when condition
>         statements for part 2
>     repeat
> [snip]

While this is certainly true, and it is a good choice for an internal
representation of loops if you're writing a compiler... I think the
original poster was more interested in the special (yet common) case
where "statements for part 1" is a trivial expression which results in
"condition". Using ":=" for assignment expression, that would look like
("pseudo-python"):

    while line := fp.readline():
        part2(line)

which is clearer for most (but certainly not all) users. There are many
"workarounds" for this "deficiency" in Python, none of which are both
fast _and_ elegant.

There's also another reason why assignment expressions are frequently
requested:

    m = re.match(...)
    if m:
        foo(m)

which according to some, would be clearer as:

    if m := re.match(...):
        foo(m)

The workarounds for the "while" case generally do not apply to the "if"
case. In any case <wink>, this has repeatedly been discussed to death.

Status quo: The BDFL likes the "while 1:" construct and doesn't like
assignment expressions, so nothing is likely to change.

-- bjorn





More information about the Python-list mailing list