Learning Python - Mark Lutz [10]
23
Context managers enabled in 2.6 and 3.0: with/as
33, 35
Exception syntax changes in 3.0: raise, except/as, superclass
33, 34
Exception chaining in 3.0: raise e2 from e1
33
Reserved word changes in 2.6 and 3.0
11
New-style class cutover in 3.0
31
Property decorators in 2.6 and 3.0: @property
37
Descriptor use in 2.6 and 3.0
31, 38
Metaclass use in 2.6 and 3.0
31, 39
Abstract base classes support in 2.6 and 3.0
28
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