Why are strings immutable?

Jeremy Bowers jerf at jerf.org
Mon Aug 23 20:39:01 EDT 2004


On Mon, 23 Aug 2004 22:11:36 +0200, Hallvard B Furuseth wrote:

> If you are doing a lot of that with the string, does it need to be a
> single string?  I've just speeded up a program significantly by changing
>   string_var = ...
>   string_var += ...
>   string_var += ...
>   ...
> to
>   array_var = ['string']
>   array_var.append(...)
>   array_var.append(...)
>   ...
>   result = "".join(array_var)
> (Well, because of how the data was structured I actually used a dict which
> was unpacked to a list comprehension which was joined to a string, but it
> still speeded things up.)

For PyDS, I contributed a class that does that and offers a += interface,
so it is easy to drop in without too much conversion work. It is very
simple.

In general, you should not use this and you should do it "right" the first
time, but for existing code this can be a convenient make-do.

Replace your initial "myString = ''" with "myString = StringCollector()",
and depending on how you use this you may not need to change the final
use. Otherwise, call "str()" on your StringCollector.

(Note the encoding in iadd; I just added the locale call without testing
it, and you may want to switch it; PyDS actually uses its own system.)

-----------------------------

import locale
class StringCollector:

        def __init__(self, string='', encoding = None):
                self.buffer = StringIO()
                if string: self += string
                if encoding in None:
                        self.encoding = locale.getpreferredencoding()
                else:
                        self.encoding = encoding
        
        def __iadd__(self, other):
                if type(string) == types.UnicodeType:
                        self.buffer.write(other.encode(self.encoding))
                else:
                        self.buffer.write(other)
                return self

        def __repr__(self):
                return '<StringCollector>'

        def __str__(self):
                return self.buffer.getvalue()




More information about the Python-list mailing list