[Python-Dev] RE: [Patches] [Patch #102915] xreadlines : readlines :: xrange : range

Thomas Wouters thomas@xs4all.net
Mon, 1 Jan 2001 23:46:45 +0100


On Mon, Jan 01, 2001 at 02:35:24PM -0500, Guido van Rossum wrote:

[ Python lacks a One True Way of doing Perl's 'while(<>)' ]

> > Does someone have an elegant way to address this?  I believe Jeff's shot at
> > elegance was the other part of the patch, using (his new) xreadlines under
> > the covers to speed the fileinput module.

> But of course suggesting fileinput is also not a great solution --
> it's relatively obscure (since it's not taught by most tutorials,
> certainly not by the standard tutorial).

Is fileinput really obscure ? I personally quite like it. It is enough like
the perl idiom to be very useful for people thinking that way, and it
doesn't require special syntax or considerations. If tutorialization is the
only problem, I'd be happy to fix that, provided Fred or Moshe can TeX my
fix up.

As for speed (which stays a secondary or tertiary consideration at best) do
we really need the xreadlines method to accomplish that ? Couldn't fileinput
get almost the same performance using readlines() with a sizehint ? I
personally don't like the xreadlines because it adds yet another function to
do the same, with a slight, subtle and to the untrained programmer unclear
distinction from the rest. (I don't really like the range/xrange difference
either -- I think Python code shouldn't care whether they're dealing with a
real list or a generator, and as much as possible should just be generators.
And in the case of simple (x)range()es, I have yet to see a case where a
'real' list had significantly better performance than a generator.)

If we *do* start adding methods to (the public API of) filemethods, I think
we should consider more than just xreadlines() (I seem to recall other
proposals, but my memory is hazy at the moment -- I haven't slept since last
millennium) add whatever is necessary, and provide a UserFile in the std.
lib that 'emulates' all fileobject functionality using a single readline()
function.

Now, if you'll excuse me, I have a date with a soft bed I haven't seen in
about 40 hours, a pair of aspirin my head is killing for and probably a
hangover that I don't want to think about, right now ;)

Gelukkig-Nieuwjaar-iedereen-ly y'rs

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!