Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is MutableString deprecated in Python?

Why was the MutableString class deprecated in Python 2.6;
and why was it removed in Python 3?

like image 455
fjsj Avatar asked Jan 10 '11 20:01

fjsj


2 Answers

The MutableString class was meant to be educational, and not to be used in real programs. If you look at the implementation, you'd see that you can't really use this in a serious application requiring mutable strings.

If you need mutable bytestrings, you might consider using bytearray that's available in Python 2.6 and 3.x. The implementation doesn't create new strings every time you modify the old one, so it is much more faster and usable. It also supports the buffer protocol properly so it can be used in place of a normal bytestring practically everywhere.

If you aren't really going to do many modifications of a single string by index, modifying a normal string by creating a new one should suit you (for example through str.replace, str.format and re.sub).

There are no mutable unicode strings, because this is considered an uncommon application, but you can always implement __unicode__ (or __str__ for Python 3) and encode methods on your custom sequence type to emulate one.

like image 51
Rosh Oxymoron Avatar answered Nov 15 '22 10:11

Rosh Oxymoron


I'm guessing because strings aren't supposed to be mutable. The primary purpose was "educational", after all. If you need to mutate strings, use a list of strings or StringIO.

like image 43
Brian Goldman Avatar answered Nov 15 '22 11:11

Brian Goldman