while true: !!!

Steve Williams sandj.williams at gte.net
Mon Dec 11 11:38:17 EST 2000


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.
>

[snip]

The two readlines are not the same.  The first tells you if the file is
empty, an important consideration in many cases.

In some applications, you use the result of the first readline to prime
control variables.

I like your loop structure, it's not ugly and I don't think it needs to
be changed.

I wouldn't call "While true" a python-idiom, I'd call it a
python-solecism.






More information about the Python-list mailing list