while true: !!!

Jaroslav Gergic j_gergic at yahoo.com
Mon Dec 11 07:55:03 EST 2000


Hello,

one thing I dislike in many Python tutorials is
the style of infinite 'while' loop with 'break' statements:

> Python:
> 
>     file = open('file.input')
> 
>     count = 0
>     while 1:
>         lines = file.readlines(8192)
>         if not lines: break
>         count = count + len(lines)
> 
>     print count
> 


I hate it so much I prefer to write something like this:

line = fh.readline()
while(line != ""):
  ... do something ...
  line = fh.readline()

OK, it is ugly to write the same line of code twice,
but 'while true' or 'while 1' is very non-programmer construction
it smells like VisualBasic GOTO.

Do you know better construction to avoid both - duplicate lines
and horrible 'while 1' construction?

Something like:

while( "" != (line = fh.readline()) ):
  ... do something ...

Thanks
Gergi



More information about the Python-list mailing list