[Python-ideas] Mutable chars objects

Talin talin at acm.org
Sun Mar 11 08:51:11 CET 2007


Something that I have wanted in Python for a long time is something like 
the Java StringBuffer class - a mutable buffer, with string-like 
methods, that holds characters instead of bytes.

I do a lot of stuff with parsing, and its often convenient to build up 
long strings of text one character at a time. Doing this with strings in 
Python is obviously not the way to go, since each time you append a 
character you have to construct a new string object. Doing it with lists 
is better, except that you still have to pay the overhead of the dynamic 
typing information for each character.

Also, unlike a list or an array, you'd ideally want something that has 
string-like methods, such as toupper() and so on. Calling str( buffer ) 
should create a string of the contents of the buffer, not generate a 
repr() of the object which is what would happen if you call str() on a 
list or array. Passing this buffer to 'print' should also just print the 
characters. Similarly, you ought to be able to comparisons between the 
mutable buffer and a real string; slices of the buffer should be 
strings, not lists, and so on.

In other words - it ought to act pretty much like STL strings.

Also, the class ought to be optimized for single-character appending, it 
should be smart enough to grow memory in the right-sized chunks; And no, 
there's no particular reason why the memory needs to be contiguous, 
although it could be.

Originally, I had thought that such a class might be called 'characters' 
(to correspond with 'bytes' in Python 3000), but it could just as easily 
be called strbuffer or something else.

-- Talin



More information about the Python-ideas mailing list