do...while loops

Fredrik Lundh fredrik at effbot.org
Tue Feb 6 03:51:49 EST 2001


Issac Trotts wrote:
> How about this:
>
> file = open(...)
> while(1):
>     line = file.readline()
>     if(line == ""): break

usually spelled:

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

(using parens after statements is bad style in Python,
and so is using == to test for an empty sequence...)

in 2.1, it's better spelled as:

    for line in file.xreadlines():
        ...

Cheers /F





More information about the Python-list mailing list