Mutable Strings?

Bastler bastler at uwnet.nl
Tue Jun 26 16:04:29 EDT 2001


Eric,

you wrote:
> 
> Guys,
> 
>   I am somewhat new to python and I have a bit of a
> question  (as a side note, at work, I am a java
> programmer mostly). In python, as in java, strings are
> immutable objects (types in python I know). In any
> case, Java has provided a StringBuffer class that is
> basically a mutable string. Allowing you to append,
> insert etc without creating a new object.  This saves
> quite a bit of object creation when any string
> processing is required.
> 
> I have been looking for similar functionality in
> python and have yet to find it. I tried searching the
> archives but any search for terms relating to strings
> returns an enormous amount of hits. In any case, are
> any of you aware of a anything in python that provides
> this type of functionality? I am willing to write my
> own I just don't want to reinvent the wheel.
> 

I wrote a jython module stringBuffer.java that wraps
a java.lang.StringBuffer in a java class file that can
be used as a jython class.

In case you are interested, let me know.
I have a .tgz file ready (unix tar gzipped),
but I could put it into other formats.

I wrote it mostly to see how extension should be done
in jython. I tried to minimize object creation.
It works, some basic tests are there,
but I have never checked  actual performance.
It could well be that the
suggested alternative of using a list of
non mutable strings is faster in most cases,
due to the way the jython interpreter works
and due to the overhead of locking done internally in
java.lang.StringBuffer.

I started writing it when I needed a buffer
for socket.recv(), which may return any number
of characters depending on network traffic
conditions.

Having written stringBuffer.java for jython
I think an alternative implementation as
a java char[] might be worth exploring
for performance, provided that there is a very
good java compiler to minimize the array
boundary checks.
As I work on powerpc's, there is no such compiler,
so I stopped.

Bastler



More information about the Python-list mailing list