function that modifies a string

tac-tics tactics40 at gmail.com
Mon Jul 10 12:33:51 EDT 2006


> >> 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?

I didn't say that. I meant that arrays in the C++ sense are dangerous.


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

C++ offers pass by value options. That makes it so you never need to
worry about messing up data that doesn't belong to you unless you
explicitly pass by reference. Python doesn't do this for you. Thus, a
mutable string class in Python requires a great deal more care since
you need to make copies of every string in every function in order to
prevent changes in one object's string from affecting another.




More information about the Python-list mailing list