while true: !!!

Steve smnordby at yahoo.com
Tue Dec 12 09:30:36 EST 2000


I dislike infinite while loops and use:

for line in file.readlines()
    ...do stuff...

No need for a break since it will automagically when it runs out of
lines.

-Steve-

Jaroslav Gergic wrote:
> 
> 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