Specific Language Extensions in 2.6 and 3.0

In general, Python 3.0 is a cleaner language, but it is also in some ways a more sophisticated language. In fact, some of its changes seem to assume you must already know Python in order to learn Python! The prior section outlined some of the more prominent circular knowledge dependencies in 3.0; as a random example, the rationale for wrapping dictionary views in a list call is incredibly subtle and requires substantial foreknowledge. Besides teaching Python fundamentals, this book serves to help bridge this knowledge gap.

Table 1 lists the most prominent new language features covered in this edition, along with the primary chapters in which they appear.

广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元

Table 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, 36

Text and binary file distinctions in 3.0

9, 36

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

31, 38

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 .

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