Speeding up: s += "string"

Daniel Dittmar daniel.dittmar at sap.com
Tue Apr 15 05:16:01 EDT 2003


Lulu of the Lotus-Eaters wrote:
> This particular slowness is not merely academic.  It's bitten me.  In
> one of his posts, I think the Martellibot even said it bit him (in the
> mists of past time).  Probably most Python programmers fell into this
> trap at least once.  I got past it, and now usually use the
> "lst.append(string); "".join(lst)" solution.  But the idea of making a
> string longer by... well, adding more to it... seems really obvious.
> Almost the "one obvious way to do it."

Doesn't cStringIO exist for this purpose? Now that extension classes can be
subclassed, adding += and str should be enough for most purposes. Perhaps
it's even possible to add a general __getattr__ to forward all string
methods to the inner string (which doesn't exist and has to be created for
each such request).

Daniel







More information about the Python-list mailing list