while c = f.read(1)

Steve Holden steve at holdenweb.com
Sun Aug 21 18:09:08 EDT 2005


Paul Rubin wrote:
> Reinhold Birkenfeld <reinhold-birkenfeld-nospam at wolke7.net> writes:
> 
>>Don't forget
>>
>>for line in f:
>>    for c in line:
>>        # do stuff
> 
> 
> As mentioned before, that's careless programming, since it can read
> the whole file into memory if the file contains no newlines.

I agree that there may be circumstances where this naiive approach is 
sub-optimal. However, calling this careless programming is a bit over 
the top. Reading files into memory is often the preferred approach 
nowadays, and on a modern laptop (say) files several megabytes in size 
will be processed perfectly happily.

Efficiency is good, but remember Kernighan and Plauger (elements of 
Programming Style): First, make it work. Then (if it doesn't work fast 
enough) make it work faster.

Most times the simple solution works for the problem under 
consideration, and useless optimization work is avoided.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/




More information about the Python-list mailing list