function that modifies a string

Diez B. Roggisch deets at nospam.web.de
Mon Jul 10 10:53:43 EDT 2006


tac-tics wrote:

> 
> 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).

Why are they more dangerous than a self-written mutable string? 
 
> 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.

I don't buy that. You are right about the dangers - but I fail to see where
C++ gives you any protection from these pitfalls. And what disqualifies an
array of characters in python that exists and has  all the methods I can
think of for a  mutable string.

Regards,

Diez



More information about the Python-list mailing list