同步阅读进度,多语言翻译,过滤屏幕蓝光,评论分享,更多完整功能,更好读书体验,试试 阅读 ‧ 电子书库
String Changes in 3.0
One of the most noticeable changes in 3.0 is the mutation of string object types. In a nutshell, 2.X’s str and unicode types have morphed into 3.0’s str and bytes types, and a new mutable bytearray type has been added. The bytearray type is technically available in Python 2.6 too (though not earlier), but it’s a back-port from 3.0 and does not as clearly distinguish between text and binary content in 2.6.
Especially if you process data that is either Unicode or binary in nature, these changes can have substantial impacts on your code. In fact, as a general rule of thumb, how much you need to care about this topic depends in large part upon which of the following categories you fall into:
In other words, if your text is always ASCII, you can get by with normal string objects and text files and can avoid most of the following story. As we’ll see in a moment, ASCII is a simple kind of Unicode and a subset of other encodings, so string operations and files “just work” if your programs process ASCII text.
Even if you fall into the last of the three categories just mentioned, though, a basic understanding of 3.0’s string model can help both to demystify some of the underlying behavior now, and to make mastering Unicode or binary data issues easier if they impact you in the future.
Python 3.0’s support for Unicode and binary data is also available in 2.6, albeit in different forms. Although our main focus in this chapter is on string types in 3.0, we’ll explore some 2.6 differences along the way too. Regardless of which version you use, the tools we’ll explore here can become important in many types of programs.
请支持我们,让我们可以支付服务器费用。
使用微信支付打赏