Why is MutableString deprecated in Python?
Why was the MutableStrin开发者_JS百科g class deprecated in Python 2.6;
and why was it removed in Python 3?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.
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.
精彩评论