The rap against "while True:" loops

Luis Zarrabeitia kyrie at uh.cu
Mon Oct 12 09:32:35 EDT 2009


On Sunday 11 October 2009 11:56:55 pm Dennis Lee Bieber wrote:
> In this situation, the middle exit works best -- using
> non-optimal Python
>
>         while True:
>                 lin = file_object.readline()
>                 if not lin: break
>                 do something with lin

Actually, in python, this works even better:

for lin in iter(file_object.readline, ""):
    ... do something with lin



-- 
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie



More information about the Python-list mailing list