同步阅读进度,多语言翻译,过滤屏幕蓝光,评论分享,更多完整功能,更好读书体验,试试 阅读 ‧ 电子书库
Specific Language Removals in 3.0
In addition to extensions, a number of language tools have been removed in 3.0 in an effort to clean up its design. Table 2 summarizes the changes that impact this book, covered in various chapters of this edition. Many of the removals listed in Table 2 have direct replacements, some of which are also available in 2.6 to support future migration to 3.0.
Table 2. Removals in Python 3.0 that impact this book
Removed
Replacement
Covered in chapter(s)
reload(M)
imp.reload(M) (or exec)
3, 22
apply(f, ps, ks)
f(*ps, **ks)
18
`X`
repr(X)
5
X <> Y
X != Y
5
long
int
5
9999L
9999
5
D.has_key(K)
K in D (or D.get(key) != None)
8
raw_input
input
3, 10
old input
eval(input())
3
xrange
range
14
file
open (and io module classes)
9
X.next
X.__next__, called by next(X)
14, 20, 29
X.__getslice__
X.__getitem__ passed a slice object
7, 29
X.__setslice__
X.__setitem__ passed a slice object
7, 29
reduce
functools.reduce (or loop code)
14, 19
execfile(filename)
exec(open(filename).read())
3
exec open(filename)
exec(open(filename).read())
3
0777
0o777
5
print x, y
print(x, y)
11
print >> F, x, y
print(x, y, file=F)
11
print x, y,
print(x, y, end=' ')
11
u'ccc'
'ccc'
7, 36
'bbb' for byte strings
b'bbb'
7, 9, 36
raise E, V
raise E(V)
32, 33, 34
except E, X:
except E as X:
32, 33, 34
def f((a, b)):
def f(x): (a, b) = x
11, 18, 20
file.xreadlines
for line in file: (or X=iter(file))
13, 14
D.keys(), etc. as lists
list(D.keys()) (dictionary views)
8, 14
map(), range(), etc. as lists
list(map()), list(range()) (built-ins)
14
map(None, ...)
zip (or manual code to pad results)
13, 20
X=D.keys(); X.sort()
sorted(D) (or list(D.keys()))
4, 8, 14
cmp(x, y)
(x > y) - (x < y)
29
X.__cmp__(y)
__lt__, __gt__, __eq__, etc.
29
X.__nonzero__
X.__bool__
29
X.__hex__, X.__oct__ X.__index__ 29
Sort comparison functions
Use key=transform or reverse=True
8
Dictionary <, >, <=, >=
Compare sorted(D.items()) (or loop code)
8, 9
types.ListType
list (types is for nonbuilt-in names only)
9
__metaclass__ = M
class C(metaclass=M):
28, 31, 39
__builtin__
builtins (renamed)
17
Tkinter
tkinter (renamed)
18, 19, 24, 29, 30
sys.exc_type, exc_value
sys.exc_info()[0], [1]
34, 35
function.func_code
function.__code__
19, 38
__getattr__ run by built-ins
Redefine __X__ methods in wrapper classes
30, 37, 38
-t, –tt command-line switches
Inconsistent tabs/spaces use is always an error
10, 12
from ... *, within a function
May only appear at the top level of a file
22
import mod, in same package
from . import mod, package-relative form
23
class MyException:
class MyException(Exception):
34
exceptions module
Built-in scope, library manual
34
thread, Queue modules
_thread, queue (both renamed)
17
anydbm module
dbm (renamed)
27
cPickle module
_pickle (renamed, used automatically)
9
os.popen2/3/4
subprocess.Popen (os.popen retained)
14
String-based exceptions
Class-based exceptions (also required in 2.6)
32, 33, 34
String module functions
String object methods
7
Unbound methods
Functions (staticmethod to call via instance)
30, 31
Mixed type comparisons, sorts
Nonnumeric mixed type comparisons are errors
5, 9
There are additional changes in Python 3.0 that are not listed in this table, simply because they don’t affect this book. Changes in the standard library, for instance, might have a larger impact on applications-focused books like Programming Python than they do here; although most standard library functionality is still present, Python 3.0 takes further liberties with renaming modules, grouping them into packages, and so on. For a more comprehensive list of changes in 3.0, see the “What’s New in Python 3.0” document in Python’s standard manual set.
If you are migrating from Python 2.X to Python 3.X, be sure to also see the 2to3 automatic code conversion script that is available with Python 3.0. It can’t translate everything, but it does a reasonable job of converting the majority of 2.X code to run under 3.X. As I write this, a new 3to2 back-conversion project is also underway to translate Python 3.X code to run in 2.X environments. Either tool may prove useful if you must maintain code for both Python lines; see the Web for details.
Because this fourth edition is mostly a fairly straightforward update for 3.0 with a handful of new chapters, and because it’s only been two years since the prior edition was published, the rest of this Preface is taken from the prior edition with only minor updating.
请支持我们,让我们可以支付服务器费用。
使用微信支付打赏