Fourth Edition Python Changes: 2.6, 3.0, 3.1

The fourth edition was updated to cover Python 3.0 and 2.6, and incorporated a small number of major changes made in 3.1. Its 3.0 and 3.1 changes apply to all future releases in the 3.X line including this fifth edition’s Python 3.3, and its 2.6 changes are also part of this edition’s 2.7. As noted earlier, some of the changes described here as 3.X changes also later found their way into Python 2.7 as back-ports (e.g., set literals, and set and dictionary comprehensions).

Changes in Python 3.1

In addition to the 3.0 and 2.6 changes listed in upcoming sections, shortly before going to press the fourth edition was also augmented with notes about prominent extensions in the then upcoming Python 3.1 release, including:

  • Comma separators and automatic field numbering in string format method calls (Chapter 7)

  • Multiple context manager syntax in with statements (Chapter 34)

  • New methods for number objects (Chapter 5)

  • (Not added until this fifth edition) Floating-point display changes (Chapter 4 and Chapter 5)

This fifth edition covers these topics in the chapters just noted. Because Python 3.1 was targeted primarily at optimization and was released relatively soon after 3.0, the fourth edition also applied directly to 3.1. In fact, because Python 3.1 superseded 3.0 entirely, and because the latest Python is usually the best Python to fetch and use anyhow, whenever that edition used the term “Python 3.0” it generally referred to the language variations introduced by Python 3.0 but that are present in the entire 3.X line, including this edition’s Python 3.3.

One notable exception: the fourth edition did not incorporate 3.1’s new repr display scheme for floating-point numbers. The new display algorithm attempts to display floating-point numbers more intelligently when possible, usually with fewer (but occasionally with more) decimal digits—a change that is reflected in this fifth edition.

Changes in Python 3.0 and 2.6

The fourth edition’s language changes stem from Python 3.0 and 2.6. All of its 2.6 and many of its 3.0 changes are shared by Python 2.7 and 3.3 today. Python 2.7 was extended with some 3.0 features not present in 2.6 (see earlier in this appendix), and Python 3.3 inherits all the features introduced by 3.0.

Because there were so many changes in the initial 3.X release, they are noted only briefly in tables here, with links to more details in this book. Table C-1 provides the first set of 3.X changes, listing the most prominent new language features covered in the fourth edition, along with the primary chapters in the current fifth edition in which they appear.

Table C-1. Extensions in Python 2.6 and 3.0

Extension

Covered in chapter(s)

The print function in 3.0

11

The nonlocal x,y statement in 3.0

17

The str.format method in 2.6 and 3.0

7

String types in 3.0: str for Unicode text, bytes for binary data

7, 37

Text and binary file distinctions in 3.0

9, 37

Class decorators in 2.6 and 3.0: @private('age')

32, 39

New iterators in 3.0: range, map, zip

14, 20

Dictionary views in 3.0: D.keys, D.values, D.items

8, 14

Division operators in 3.0: remainders, / and //

5

Set literals in 3.0: {a, b, c}

5

Set comprehensions in 3.0: {x**2 for x in seq}

4, 5, 14, 20

Dictionary comprehensions in 3.0: {x: x**2 for x in seq}

4, 8, 14, 20

Binary digit-string support in 2.6 and 3.0: 0b0101, bin(I)

5

The fraction number type in 2.6 and 3.0: Fraction(1, 3)

5

Function annotations in 3.0: def f(a:99, b:str)->int

19

Keyword-only arguments in 3.0: def f(a, *b, c, **d)

18, 20

Extended sequence unpacking in 3.0: a, *b = seq

11, 13

Relative import syntax for packages enabled in 3.0: from .

24

Context managers enabled in 2.6 and 3.0: with/as

34, 36

Exception syntax changes in 3.0: raise, except/as, superclass

34, 35

Exception chaining in 3.0: raise e2 from e1

34

Reserved word changes in 2.6 and 3.0

11

New-style class cutover in 3.0

32

Property decorators in 2.6 and 3.0: @property

38

Descriptor use in 2.6 and 3.0

32, 38

Metaclass use in 2.6 and 3.0

32, 40

Abstract base classes support in 2.6 and 3.0

29

Specific Language Removals in 3.0

In addition to extensions, a number of 2.X language tools have been removed in 3.X in an effort to clean up its design. Table C-2 summarizes the 3.X removals that impact this book, covered in various chapters of this edition as noted. As also shown in this table, many of the 3.X removals have direct replacements, some of which are also available in 2.6 and 2.7 to support future migration to 3.X.

Table C-2. Removals in Python 3.0 that impact this book

Removed

Replacement

Covered in chapter(s)

reload(M)

imp.reload(M) (or exec)

3, 23

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

13, 14

file

open (and io module classes)

9

X.next

X.__next__, called by next(X)

14, 20, 30

X.__getslice__

X.__getitem__ passed a slice object

7, 30

X.__setslice__

X.__setitem__ passed a slice object

7, 30

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' (back in 3.3)

'ccc'

4, 7, 37

'bbb' for byte strings

b'bbb'

4, 7, 9, 37

raise E, V

raise E(V)

33, 34, 35

except E, X:

except E as X:

33, 34, 35

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)

30

X.__cmp__(y)

__lt__, __gt__, __eq__, etc.

30

X.__nonzero__

X.__bool__

30

X.__hex__, X.__oct__

X.__index__

30

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 non-built-in names only)

9

__metaclass__ = M

class C(metaclass=M):

29, 32, 40

__builtin__

builtins (renamed)

17

Tkinter

tkinter (renamed)

18, 19, 25, 30, 31

sys.exc_type, exc_value

sys.exc_info()[0], [1]

35, 36

function.func_code

function.__code__

19, 39

__getattr__ run by built-ins

Redefine __X__ methods in wrapper classes

31, 38, 39

-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

23

import mod, in same package

from . import mod, package-relative form

24

class MyException:

class MyException(Exception):

35

exceptions module

Built-in scope, library manual

35

thread, Queue modules

_thread, queue (both renamed)

17

anydbm module

dbm (renamed)

28

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)

33, 34, 35

String module functions

String object methods

7

Unbound methods

Functions (staticmethod to call via instance)

31, 32

Mixed type comparisons, sorts

Nonnumeric mixed type magnitude comparisons (and sorts) are errors

5, 9