预计阅读本页时间:-
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
The nonlocal x,y statement in 3.0
The str.format method in 2.6 and 3.0
String types in 3.0: str for Unicode text, bytes for binary data
Text and binary file distinctions in 3.0
Class decorators in 2.6 and 3.0: @private('age')
New iterators in 3.0: range, map, zip
Dictionary views in 3.0: D.keys, D.values, D.items
Division operators in 3.0: remainders, / and //
Set literals in 3.0: {a, b, c}
Set comprehensions in 3.0: {x**2 for x in seq}
Dictionary comprehensions in 3.0: {x: x**2 for x in seq}
Binary digit-string support in 2.6 and 3.0: 0b0101, bin(I)
The fraction number type in 2.6 and 3.0: Fraction(1, 3)
Function annotations in 3.0: def f(a:99, b:str)->int
Keyword-only arguments in 3.0: def f(a, *b, c, **d)
Extended sequence unpacking in 3.0: a, *b = seq
Relative import syntax for packages enabled in 3.0: from .
Context managers enabled in 2.6 and 3.0: with/as
Exception syntax changes in 3.0: raise, except/as, superclass
Exception chaining in 3.0: raise e2 from e1
Reserved word changes in 2.6 and 3.0
New-style class cutover in 3.0
Property decorators in 2.6 and 3.0: @property
Descriptor use in 2.6 and 3.0
Metaclass use in 2.6 and 3.0
Abstract base classes support in 2.6 and 3.0