Why is MutableString deprecated in Python?

江枫思渺然 提交于 2019-12-03 05:49:31

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!