function that modifies a string

tac-tics tactics40 at gmail.com
Mon Jul 10 10:37:46 EDT 2006


Diez B. Roggisch wrote:
> > Of course, another right way would be to have mutable strings in Python.
> > I understand why strings need to be immutable in order to work with dicts,
> > but is there any reason why (hypothetical) mutable strings should be
> > avoided in situations where they aren't needed as dictionary keys? Python
> > has mutable lists and immutable tuples, mutable sets and immutable frozen
> > sets, but no mutable string type.
>
> What's wrong about arrays of chars?

Arrays of chars are dangerous. If you insist, use Python lists of
Python "chars" (strings of length 1).

If you really want a mutable string type, there's nothing in python
that keeps you from writting one yourself. You just have to be more
careful than you would be in C++, because your MutableString type would
always be passed by reference to functions, and so you'd have to be
very careful to copy it unless you want weird, unfindable bugs to crop
up in your program.




More information about the Python-list mailing list