预计阅读本页时间:-
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
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
Removed
Replacement
Covered in chapter(s)
reload(M)
imp.reload(M) (or exec)
apply(f, ps, ks)
f(*ps, **ks)
`X`
repr(X)
X <> Y
X != Y
long
int
9999L
9999
D.has_key(K)
K in D (or D.get(key) != None)
raw_input
input
old input
eval(input())
xrange
range
file
open (and io module classes)
X.next
X.__next__, called by next(X)
X.__getslice__
X.__getitem__ passed a slice object
X.__setslice__
X.__setitem__ passed a slice object
reduce
functools.reduce (or loop code)
execfile(filename)
exec(open(filename).read())
exec open(filename)
exec(open(filename).read())
0777
0o777
print x, y
print(x, y)
print >> F, x, y
print(x, y, file=F)
print x, y,
print(x, y, end=' ')
u'ccc'
'ccc'
'bbb' for byte strings
b'bbb'
raise E, V
raise E(V)
except E, X:
except E as X:
def f((a, b)):
def f(x): (a, b) = x
file.xreadlines
for line in file: (or X=iter(file))
D.keys(), etc. as lists
list(D.keys()) (dictionary views)
map(), range(), etc. as lists
list(map()), list(range()) (built-ins)
map(None, ...)
zip (or manual code to pad results)
X=D.keys(); X.sort()
sorted(D) (or list(D.keys()))
cmp(x, y)
(x > y) - (x < y)
X.__cmp__(y)
__lt__, __gt__, __eq__, etc.
X.__nonzero__
X.__bool__
X.__hex__, X.__oct__ X.__index__ 29
Sort comparison functions
Use key=transform or reverse=True
Dictionary <, >, <=, >=
Compare sorted(D.items()) (or loop code)
types.ListType
list (types is for nonbuilt-in names only)
__metaclass__ = M
class C(metaclass=M):
__builtin__
builtins (renamed)
Tkinter
tkinter (renamed)
sys.exc_type, exc_value
sys.exc_info()[0], [1]
function.func_code
function.__code__
__getattr__ run by built-ins
Redefine __X__ methods in wrapper classes
-t, –tt command-line switches
Inconsistent tabs/spaces use is always an error
from ... *, within a function
May only appear at the top level of a file
import mod, in same package
from . import mod, package-relative form
class MyException:
class MyException(Exception):
exceptions module
Built-in scope, library manual
thread, Queue modules
_thread, queue (both renamed)
anydbm module
dbm (renamed)
cPickle module
_pickle (renamed, used automatically)
os.popen2/3/4
subprocess.Popen (os.popen retained)
String-based exceptions
Class-based exceptions (also required in 2.6)
String module functions
String object methods
Unbound methods
Functions (staticmethod to call via instance)
Mixed type comparisons, sorts
Nonnumeric mixed type comparisons are errors
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.