What’s New In “Dive Into Python 3” Python 3中的新玩意儿

 

What’s New In “Dive Into Python 3”Python 3中的新玩意儿

 

Isn’t this where we came in?
— Pink Floyd, The Wall

 这还是我们以前用的python么?
— Pink Floyd, The Wall

 

 

a.k.a. “the minus level”

导读

 

You read the original “Dive Into Python” and maybe even bought it on paper. (Thanks!) You already know Python 2 pretty well. You’re ready to take the plunge into Python 3. … If all of that is true, read on. (If none of that is true, you’d be better off starting at the beginning.)

如果读过甚至买过以前的"Dive Into Python"(谢谢您的支持),并且已经对Python2了解得很好,而现在又打算投身于Python3,那么请继续往下阅读。如果不是这样,最好从 starting at the beginning 章节开始看。

 

Python 3 comes with a script called 2to3. Learn it. Love it. Use it. Porting Code to Python 3 with 2to3 is a reference of all the things that the 2to3 tool can fix automatically. Since a lot of those things are syntax changes, it’s a good starting point to learn about a lot of the syntax changes in Python 3. (print is now a function, `x` doesn’t work, &c.)

Python3中加入了一个名为 2to3 的脚本。爱上它,并且用好它。 Porting Code to Python 3 with 2to3 (使用 2to3 将代码移植到python3)讲述了 2to3 可以自动完成修补的所有东西,这些东西大部分都是语法的变化,从一开始就了解这些东西,是一个好的开始。print现在是函数了,'x'不在继续使用了。

 #T#`x` doesn’t work,'x'是什么东西???????

 

 

Case Study: Porting chardet to Python 3 documents my (ultimately successful) effort to port a non-trivial library from Python 2 to Python 3. It may help you; it may not. There’s a fairly steep learning curve, since you need to kind of understand the library first, so you can understand why it broke and how I fixed it. A lot of the breakage centers around strings. Speaking of which…

Case Study: Porting chardet to Python 3(案例研究:chardet 到 Python3 的移植)中讲述了我如何成功地将一个并不那么简单的库,从Python2移植到Python3的过程,这或许对你有所帮助。这一过程所讲述的内容学习起来会有点吃力,需要首先对这个库有一定的了解,这样才知道它为什么不能继续使用,以及我是如何修改它的。A lot of the breakage centers around strings(在移植过程中,围绕着字符串会存在许多问题)。

Strings. Whew. Where to start. Python 2 had “strings” and “Unicode strings.” Python 3 has “bytes” and “strings.” That is, all strings are now Unicode strings, and if you want to deal with a bag of bytes, you use the new bytes type. Python 3 will never implicitly convert between strings and bytes, so if you’re not sure which one you have at any given moment, your code will almost certainly break. Read the Strings chapter for more details.

说到字符串。从哪里开始讲呢。python2有“strings” 和 “Unicode strings” 。Python3 有“bytes”和“strings”,也就是说在python3中所有字符串现在都是Unicode strings,如果想处理字节,就要使用新增加的 bytes 类型。Python3绝不会偷偷自己在字符串和字节两种类型间转换,因此如果你使用的时候不加以区分,那么你的代码几乎一定会有问题。有关详细信息,请阅读 the Strings chapter  一章。

Bytes vs. strings comes up again and again throughout the book.

字节和字符串的对比会在贯穿本书。

  • In Files, you’ll learn the difference between reading files in “binary” and “text” mode. Reading (and writing!) files in text mode requires an encoding parameter. Some text file methods count characters, but other methods count bytes. If your code assumes that one character == one byte, it will break on multi-byte characters.
  • Files中,你会了解到使用“binary” 和“text”模式读取文件的不同。在text模式下读写文件,需要encoding参数。有些处理text文件的方法会按字符计算,还有一些按字节计算,比如,utf32用4个字节表示一个字符。这种情况下,如果你程序假定字符与字节没有区别,那么像处理utf32这种字符时就会有问题。
 
 
  • In HTTP Web Services, the httplib2 module fetches headers and data over HTTP. HTTP headers are returned as strings, but the HTTP body is returned as bytes.
  • HTTP Web Services中,httplib2 模块通过HTTP获取头和数据。HTTP头会作为string返回,而HTTP内容则作为byte返回。
 
 
  • In Serializing Python Objects, you’ll learn why the pickle module in Python 3 defines a new data format that is backwardly incompatible with Python 2. (Hint: it’s because of bytes and strings.) Also JSON, which doesn’t support the bytes type at all. I’ll show you how to hack around that.
  • Serializing Python Objects中,你会看到python3 中的pickle 模块为何定义了一种新的且不兼容python2的格式,其实这么做就是因为byte和string的问题。还有JSON,为什么对bytes丝毫不支持。同时,我也会告诉你如何处理这些问题。
 

Even if you don’t care about Unicode (oh but you will), you’ll want to read about string formatting in Python 3, which is completely different from Python 2.

即使你不关心Unicode(坦白讲,你早晚都要接触他),你也要读string formatting in Python 3,这里面的东西与python2完全不同。

Iterators are everywhere in Python 3, and I understand them a lot better than I did five years ago when I wrote “Dive Into Python”. You need to understand them too, because lots of functions that used to return lists in Python 2 will now return iterators in Python 3. At a minimum, you should read the second half of the Iterators chapter and the second half of the Advanced Iterators chapter.

python3中Iterators也是无处不在,相比5年前我写“Dive Into Python”的时候,我对Iterators了解的更好了。你也需要了解他们,因为python2中许多用来返回列表的函数,在python3中返回的是Iterators。你最起码也应当读读the second half of the Iterators chapterthe Iterators chapter的第二部分) 和 the second half of the Advanced Iterators chapterthe Advanced Iterators 的第二部分).

 

By popular request, I’ve added an appendix on Special Method Names, which is kind of like the Python docs “Data Model” chapter but with more snark.

根据大家热烈要求,我在附录中增加了关于 Special Method Names(特殊方法名)的内容,这有点像the Python docs “Data Model” chapter 一章,不同的是多了许多好玩意儿

When I was writing “Dive Into Python”, all of the available XML libraries sucked. Then Fredrik Lundh wrote ElementTree, which doesn’t suck at all. The Python gods wisely incorporated ElementTree into the standard library, and now it forms the basis for my new XML chapter. The old ways of parsing XML are still around, but you should avoid them, because they suck!

我当时写“Dive Into Python”时, 所有可用的XML库都不好使。Fredrik Lundh 写的ElementTree也不好使。Python明智的将ElementTree 加入到标准库中 (incorporated ElementTree into the standard library),也就是现在my new XML chapter 的基础。以前解析XML的就方法还有,但应当避免使用,因为不好使。

 

Also new in Python — not in the language but in the community — is the emergence of code repositories like The Python Package Index (PyPI). Python comes with utilities to package your code in standard formats and distribute those packages on PyPI. Read Packaging Python Libraries for details.

Python的另一新变化不是语言而是社区,现在出现了类似 The Python Package Index (PyPI)的代码仓库。Python现在用标准格式打包你的代码并在PyPI上发布。详细信息,请参阅Packaging Python Libraries

 

以上内容全部由kuramantboy@hotmail.com翻译,读完第一章我在想也许我们应该从Python3开始,而不是现在的2.6。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值