Much of this appendix relates Python changes to book coverage. If you’re instead looking for a quick summary of the most prominent 2.X/3.X distinctions, the following may suffice. Note that this section primarily compares the latest 3.X and 2.X releases—3.3 and 2.7. Many 3.X features are not listed here because they were either also added to 2.6 (e.g., the with statement and class decorators), or back-ported later to 2.7 (e.g., set and dictionary comprehensions), but are not available in earlier 2.X releases. See later sections for more fine-grained information about changes in earlier versions, and see Python’s “What’s New” documents for changes that may appear in future releases.
The following summarizes tools that differ across Python lines.
Unicode string model: In 3.X, normal str strings support all Unicode text including ASCII, and the separate bytes type represents raw 8-bit byte sequences. In 2.X, normal str strings support both 8-bit text including ASCII, and a separate unicode type represents richer Unicode text as an option.
File model: In 3.X, files created by open are specialized by content—text files implement Unicode encodings and represent content as str strings, and binary files represent content as bytes strings. In 2.X, files use distinct interfaces—files created by open represent content as str strings for content that is either 8-bit text or bytes-based data, and codecs.open implements Unicode text encodings.
Class model: In 3.X, all classes derive from object automatically and acquire the numerous changes and extensions of new-style classes, including their differing inheritance algorithm, built-ins dispatch, and MRO search order for diamond-pattern trees. In 2.X, normal classes follow the classic model, and explicit inheritance from object or other built-in types enables the new-style model as an option.
Built-in iterables: In 3.X, map, zip, range, filter, and dictionary keys, values, and items are all iterable objects that generate values on request. In 2.X, these calls create physical lists.
Printing: 3.X provides a built-in function with keyword arguments for configuration, while 2.X provides a statement with special syntax for configuration.
Relative imports: Both 2.X and 3.X support from . relative import statements, but 3.X changes the search rule to skip a package’s own directory for normal imports.
True division: Both 2.X and 3.X support the // floor division operator, but the / is true division in 3.X and retains fractional remainders, while / is type-specific in 2.X.
Integer types: 3.X has a single integer type that supports extended precision. 2.X has both normal int and extended long, and automatic conversion to long.
Comprehension scopes: In 3.X, all comprehension forms—list, set, dictionary, generator—localize variables to the expression. In 2.X, list comprehensions do not.
PyDoc: An all-browser pydoc –b interface is supported as of 3.2 and required as of 3.3. In 2.X, the original pydoc –g GUI client interface may be used instead.
Byte code storage: As of 3.2, 3.X stores byte code files in a __pycache__ subdirectory of the source directory, with version-identifying names. In 2.X, byte code is stored in the source file directory with generic names.
Built-in system exceptions: As of 3.3, 3.X has a reworked exception hierarchy for OS and IO classes that includes additional categories and granularity. In 2.X, exception attributes must sometimes be inspected on system errors.
Comparisons and sorts: In 3.X, relative magnitude comparisons of both mixed-types and dictionaries are errors, and sorts do not support mixed types or general comparison functions (use key mappers instead). In 2.X all these forms work.
String exceptions and module functions: String-based exceptions are fully removed in 3.X, though they are also gone in 2.X as of 2.6 (use classes instead). string module functions redundant with string object methods are also removed in 3.X.
Language removals: Per Table C-2, 3.X removes, renames, or relocates many 2.X language items: reload, apply, `x`, <>, 0177, 999L, dict.has_key, raw_input, xrange, file, reduce, and file.xreadlines.
The following summarizes tools available in 3.X only.
Extended sequence assignment: 3.X allows a * in sequence assignment targets to collect remaining unmatched iterable items in a list. 2.X can achieve similar effects with slicing.
Nonlocal: 3.X provides a nonlocal statement, which allows names in enclosing function scopes to be changed from within nested functions. 2.X can achieve similar effects with function attributes, mutable objects, and class state.
Function annotations: 3.X allows function arguments and return types to be annotated with objects that are retained in the function but not otherwise used. 2.X may often achieve similar effects with extra objects or decorator arguments.
Keyword-only arguments: 3.X allows specification of function arguments that must be passed as keywords, typically used for extra configuration options. 2.X may often achieve similar effects with argument analysis and dictionary pops.
Chained exceptions: 3.X allows exceptions to be chained and thus appear in error messages, with a raise from extension; 3.3 allows a None to cancel the chain.
Yield from: As of 3.3, the yield statement may delegate to a nested generator with from. 2.X can often achieve similar results with a for loop in simpler use cases.
Namespace packages: As of 3.3, the package model is extended to allow packages that span multiple directories with no initialization file, as a fallback option. 2.X might achieve similar effects with import extensions.
Windows launcher: As of 3.3, a launcher is shipped with Python for Windows, though this is also available separately for use on other Pythons, including 2.X.
Internals: As of 3.2, threading is implemented with time slices instead of virtual machine instruction counts, and 3.3 stores Unicode text in a variable-length scheme instead of fixed-size bytes. 2.X’s string model minimizes Unicode use in general.