two ideoms at one blow: line-reading and regexp-matching

François Pinard pinard at
Wed Feb 20 10:27:49 EST 2002

[Russell Nelson]

> There are two Python ideoms that bother me:

>     while 1:
>         line = file.readline()
>         if not line: break

Hello, Russell.  Glad to see you here as well! :-)

For the above, I like writing:

    line = file.readline()
    while line:
        line = file.readline()

even if I agree that there are cases it would be more awkward to do so,
like for example, when one does not want to conceal the line reading code
into a function.

> I'd really, really like it if you could do one(1) assignment within
> the boolean expression in a while and an if.  Like this:

>     while line = file.readline():

I would rather not see Python accepting this, first because it makes the
code less explicit, but also because it would more easily allow the error
of miswriting `==' as `=' unnoticed by the Python code reader.

> I just think it would be easier to read programs if they could be
> written like this.

Programs may get slightly easier to write, I doubt it would really make
them more legible, however.  My feeling is that the current situation is
better than alternatives, all considered.

François Pinard

More information about the Python-list mailing list