[Python-Dev] Re: Optional separatorargument for file.writelines()and StringIO.writelines()

Jeremy Fincher fincher.8 at osu.edu
Thu Feb 26 21:40:27 EST 2004


On Feb 26, 2004, at 8:13 PM, Raymond Hettinger wrote:

>> This is a big YES!
>
> Actually, it's a big no.  Alex, immediately and correctly pointed out
> that what is needed in a suffix rather than a separator and the way to
> get that is with a generator expression:  f.writelines(x+'\n' for x in
> mylines).

That'll still cause the copying of every string in the iterable; 
interleaving the newline with the strings themselves would probably be 
a faster solution, I think.

> I had been led astray because I was experimenting with using
> cStringIO.writelines() as a basis for implementing str.join() for
> general iterables without creating an intermediate tuple.  Right now,
> ''.join(it) will unexpectedly consume much more memory than really
> needed.

This sounds like a cool idea, are you still going to implement it?

Jeremy




More information about the Python-Dev mailing list