Editable strings anyone?

Jason Orendorff jason at jorendorff.com
Sun Feb 17 13:02:26 EST 2002


> I'm wondering how difficult it would be to add an editable (mutable)
> string class to Python 2.2 implemented in C under the new type/class
> unification scheme.

It would be a pain.  I think you'd find that you can't really
reuse much (if any) of the code from stringobject.c, since you're
changing the underlying representation of the data in memory.

I don't think you could subclass str to do this at all; you'd
want to create a new type.

On the other hand, creating a new type isn't so tough.  If str
and list didn't have 3 dozen methods each, I'd say it's a breeze.  :)


> [...] Does this sound like a reasonable design?  Does it fit in
> reasonably with the CPython implementation?  Would it require a
> lot of hacking to existing string functions like the re module?

No, this sounds good, and everything should work okay.  I think
you would have to sneakily close the gap whenever the re module
asks you for your buffer pointer, because (I think) re needs a
contiguous buffer.  Not sure about that.

> Python surprisingly lacks a counterpart to StringBuffer.
> Python programmers tend to say either a=b+c+d+e+f (potentially
> taking quadratic time in the total number of characters) or some
> kludge like a=''.join(b,c,d,e,f).

Lately I've been using str.join.  My first impression was that it's
a hack, but I've changed my mind.  It's surprisingly flexible.
I can tinker with the list - even sort() or reverse() it - and then
at the end, I get whatever separator I choose.  Nice.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list