"".join(string_generator()) fails to be magic

Matt Mackal mpm at selenic.com
Thu Oct 11 08:26:04 CEST 2007

I have an application that occassionally is called upon to process
strings that are a substantial portion of the size of memory. For
various reasons, the resultant strings must fit completely in RAM.
Occassionally, I need to join some large strings to build some even
larger strings.

Unfortunately, there's no good way of doing this without using 2x the
amount of memory as the result. You can get most of the way there with
things like cStringIO or mmap objects, but when you want to actually
get the result as a Python string, you run into the copy again.

Thus, it would be nice if there was a way to join the output of a
string generator so that I didn't need to keep the partial strings in
memory. <subject> would be the obvious way to do this, but it of
course converts the generator output to a list first.

 "Love the dolphins," she advised him. "Write by W.A.S.T.E.." 

More information about the Python-list mailing list