20

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

Honest Abe
  • 8,430
  • 4
  • 49
  • 64
fjsj
  • 10,995
  • 11
  • 41
  • 57
  • "The main intention of this class is to serve as an educational example for inheritance..." – Anon. Jan 10 '11 at 20:51
  • Protected since I just linked to this on Meta in regards to another discussion. Preserving it as originally asked. – Thomas Owens Sep 16 '11 at 22:31

2 Answers2

26

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.

Rosh Oxymoron
  • 20,355
  • 6
  • 41
  • 43
5

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.

Brian Goldman
  • 716
  • 3
  • 11