Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
808
|
26 章 v2/v3 的移轉與共存
請注意,它並非永遠都像看起來那樣是萬靈丹(而在此它當然沒有提供立
即的解藥,如我們稍後會看到的)。雖然在這種情況中它是有幫助的,但
如果程式混有二進位和文字字串資料,就沒有一種能夠同時滿足兩種用途
的表示方式,而許多開發人員偏好循序漸進。這些變更可能在 Python 2
造成問題,這是需要被解決的。不只如此,所有的 docstrings 也會因此隱
含地變為 Unicode,而那在 2.7.7 之前會導致語法錯誤。
單是這個變更,實際上可能會使失敗的測試數目
增加
為三個,而這個錯誤
在所有情況中都是相同的:
File "/Users/sholden/Projects/Python/voitto/tappio/lexer.py",
line 45, in build_set
assert type(ch) in (str, str)
你可以看到
2to3
移除
unicode
型別的變更反過來咬了我們一口,因為雖然
那在 v3 底下是可行的,但在 v2 底下,我們需要明確地允許型別為
unicode
的字串。我們不能只是回復那個變更,因為在 v3 底下沒有該型別可用:試
著參考它會提出
NameError
例外。
我們需要的是兩個版本都相容的一種型別。
six
模組(稍後涵蓋)確實有
引進這樣的型別,但我們沒有選擇承受那整個模組帶來的額外負擔,在此
我們選擇撰寫一個簡單的相容性模組
tappio/compat.py
,它會為執行中的版
本建立
unicode
這個名稱的適當定義。依照移植指南( ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

利用 Python 进行数据分析(原书第2版)

利用 Python 进行数据分析(原书第2版)

Wes McKinney

Publisher Resources

ISBN: 9789864766819