"in" for dicts (was: Python 2.1 function attributes)

Tim Peters tim.one at home.com
Mon Jan 29 07:19:42 EST 2001


[Remco Gerlich]
> Huh? I like it (xreadlines) because it's a very simple way to
> iterate over a  file line by line without having the whole file
> in memory. Simpler than the several existing ways that get
> discussed over and over and over.

    for line in f.readlines():

would have been almost infinitely preferable, had we been able to dream up a
reasonable way to do that lazily that wouldn't break some existing code.
But we couldn't.

As is,

   for line in fileinput.input(fname):

is even simpler (it opens the file for you), and can do all sorts of nifty
common file operations with some more arguments (iterate over multiple
files, keep track of line numbers, do in-place modifications w/ or w/o
backups, parse a cmdline).  The only reason to like .xreadlines() is
speed -- but, again, that's reason enough.

> And it's faster, too!!

cross-out-the-"too"-and-"bingo!"-ly y'rs  - tim





More information about the Python-list mailing list