Mutable strings

Hans-Joachim Widmaier hjwidmaier at web.de
Mon Sep 22 06:51:31 EDT 2003


Andy Jewell <andy at wild-flower.co.uk> wrote in message news:<mailman.1064086468.9586.python-list at python.org>...

> Mutable strings are one thing that I missed, initially, when I first star
> ted 
> using Python.  After a while, as the "Pythonic" way of doing things sank 
> in, 
> I realised that Python doesn't *need* mutable strings.

Mutable strings come to *my* mind whenever I have to play with huge
binary data.  Working with tens of megabytes is inherently somewhat
slow.

> Python strings (and integers and floats) are all immutable for a very goo
> d  
> reason:  dictionaries can't reliably use mutable objects as keys.

All understood. But then, I don't want to use my 32-MB binary blob as
a key.

> however, once I fully 
> understood the % (percent) string operator, and the ability to efficiently
> convert strings into lists and back, my anxiety went away.   These cover 
> most usage of strings that might convince you you need mutability.

Converting said blob 'efficiently' to a list is something that I
certainly would not call 'efficiently' - if not for the conversion
itself, then for the memory consumption as list.

I don't think strings are immutable because they ought to be that way
(e.g. some CS guru teaches that "mutable strings are the root of all
evil"). They're immutable because they allow them to be used as
dictionary keys. And it was found that this doesn't affect the
usefulness of the language too much.

Still, I can see a use for mutable strings. Or better, mutable binary
data, made up of bytes. (where 'byte' is the smallest individually
addressable memory unit blabla, ... you get the meaning. Just to not
invite nit-pickers on that term.)

> "explicit is better than implicit".

Yes, definitely: Let there be another type.

Byte-twiddlingly yours,
Hans-J.




More information about the Python-list mailing list