while true: !!!

Steve Williams sandj.williams at gte.net
Tue Dec 12 13:16:25 EST 2000


Alex Martelli wrote:

> "Steve" <smnordby at yahoo.com> wrote in message
> news:3A36368C.26AADDDE at yahoo.com...
> > 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.
>
> Surely best, *IF* you're 100% sure that all the files you
> ever read will always fit comfortably in memory.
>
> If you must provide for reading huge files, this will just
> not work.
>

[snip]

I think we're building a strawman here--we all know readlines() holds a
trap for the unwary.

The 'delightful' example would be

    While 1:
        recordSet = readline(intComfortableNumber)
        if not recordSet:
            break
        for record in recordSet:
            do your thing

The 'convenient' example would be:

    recordSet = readline(intComfortableNumber)
     while recordSet:
          for record in recordSet:
              do your thing
          recordSet = readline(intComfortableNumber)

Perhaps




More information about the Python-list mailing list