Speeding up: s += "string"

Lulu of the Lotus-Eaters mertz at gnosis.cx
Tue Apr 15 13:31:48 EDT 2003


"Daniel Dittmar" <daniel.dittmar at sap.com> wrote previously:
|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).

cStringIO.StringIO cannot be subclassed (at least through 2.3a2).  Maybe
it could be implemented in a subclassable way, but it is not now.  The
Python version can be subclassed, but presumably it is much slower (not
in a big-O sense, but as practical times).

In the abstract, StringIO has much of the behavior I would want; but it
always feels kind cumbersome to use.  I usually find that building and
joining a list of strings feels less heavy.

Assuming StringIO were to get some new methods, there are quite a few
you'd want to worry about.  .__iadd__() is one.  .__repr__() and/or
.__str__() might be desireable to make "print sio" more friendly.  And
slicing.  And string methods like .upper() or .find().  (OK, Dittmar
mentions the __getattr__() approach, but again, that is a big time
cost).

Yours, Lulu...

--
 mertz@  _/_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: \_\_\_\_    n o
gnosis  _/_/             Postmodern Enterprises            \_\_
.cx    _/_/                                                 \_\_  d o
      _/_/_/ IN A WORLD W/O WALLS, THERE WOULD BE NO GATES \_\_\_ z e






More information about the Python-list mailing list