Scope ===== * Mainly builtins and language. Only major library module additions mentioned. * Thing new in 2.3, 2.4 and 2.5. 2.2 mentioned where it began a process continued later. Types ===== PEP 238: Changing the Division Operator --------------------------------------- 2.2 ``1 // 2 == 0`` is explicit floor division. ``from __future__ import division`` enables "true division": ``1 / 2 == 0.5``. Standard library already clean for ``-Qnew`` mode. 3.0? True division becomes default. PEP 237: Unifying Long Integers and Integers -------------------------------------------- 2.2 What used to raise `OverflowError` (e.g. ``2 ** 40``), automatically converts to long. 'L' suffix is optional. 2.3 What used to silently lose bits (e.g. ``1 << 40``), still does but prints `FutureWarning`. 2.4 No warning, long behavior completely wins. PEP 285: A Boolean Type ----------------------- 2.2.1 [*]_ False, True = 0, 1 2.3 True + True == 2 forever but str(bool(1)) == "True" .. [*] Bad idea to introduce a feature in a minor version, especially since it's an incomplete feature. Enough code ended up testing for it :-(. Strong policy of no features in minor versions since. PEP 218: A Standard Set Datatype -------------------------------- 2.3 (the module works on 2.2 if you copy it) Python `sets` module: `sets.Set()`, `sets.ImmutableSet()`. Supports boolean algebra operations. `dict.fromkeys(keys, value=None)` builds set-like dicts. 2.4 Builtin types implemented in C: `set()`, `frozenset()` (same thing except for auto-freezing keys). The `sets` module remains for compatibility. 2.5 Builtin `set()`, `frozenset()` optimized (not internally based on dict now). Sorting goodies --------------- 2.3 `list.sort()` heavily optimized. 2.4 `sorted(iterable)` returns new sorted list. Keyword arguments `cmp`, `key`, `reverse` make common cases easier. `operator.attrgetter()` and `operator.itemgetter()` handy as `key` functions. 2.5 `min()` and `max()` gain `key` arguments. `operator.attrgetter()` and `operator.itemgetter()` support multiple arguments (return tuples). Importing modules ================= 2.3 PEP 302: New Import Hooks: `sys.path` can contain objects; `sys.meta_path` and `sys.path_hooks` allow flexible weird things. 2.4 PEP 328: Multi-line Imports PEP 328: Absolute/Relative Imports: ``from __future__ import absolute_import`` stops looking relative to current `__file__` unless the import is explicitly relative (``from .dir import module``). 2.5 Implicitly relative imports will give `DeprecationWarning`. 2.6 All imports will be absolute by default. Iterators and generators ======================== PEP 234: Iterators ------------------ 2.2 New `iterable.__iter__()` protocol returning an iterator that has `iterator.next()` until it raises `StopIteration`. New `iter()` builtin. ``for`` loop now uses this protocol. So do many constructors (e.g. `list()` and functions (e.g. `max`). Iterators for builtin types: files (lines), sequences, dicts (keys). 2.3 `itertools` module with things like `chain`, `ifilter`, `izip`. ``enumarate(["a", "b", "c"])`` yields (0, "a"), (1, "b"), (2, "c") 2.4 PEP 322: Reverse Iteration: ``for i in reversed(sequence)``. PEP 323: Copyable Iterators: `iterator.__copy__()`, `itertools.tee()`. 2.5 Cleanup after iteration: `iterator.close()`, `iterator.throw(exc)`. Co-routines: `iterator.send(value)` instead of `iterator.next()`. PEP 255: Simple Generators -------------------------- 2.2 ``yield`` inside function makes it return a generator object - cool way to implement iterators. Requires ``from __future__ import generators``. 2.3 ``yield`` availiable without a ``__future__`` import. 2.4 PEP 289: Generator Expressions: ``sum(x**2 for x in range(5))``. 2.5 ``yield`` responds to `generator.close()` and `generator.throw()`. Can now yield inside ``try..finally``. ``yield`` is an expression, responds to `generator.send(value)`. Reduction --------- 2.3 `sum()` builting added and starts a trend of reduction functions. 2.5 `any()` and `all()` boolean reduction functions added. Other syntax ============ PEP 318: Decorators for Functions and Methods --------------------------------------------- 2.2 `staticmethod()`, `classmethod()` wrappers for methods. 2.4 ``@decorator`` before functions. Note: `property()` still not handled cleanly, metaclasses still ugly black magic. This is a very open field. PEP 343: The "with" Statement ----------------------------- 2.5 [details still not final] ``with`` statement to abstract enter-exit patterns. Will have it's own `__enter__()`/`__exit__()` protocol but a standard decorator would wrap generators as ``with`` context functions. Conditional expressions ----------------------- 2.2 ~ 2.3 Lots of discussion on syntax, indecisive voting => nothing done. 2.5 ``THEN if COND else ELSE`` pronounced by Guido. No fuss. Major new, modules ================== 2.3 `optparse`, `datetime`, `logging`, `csv` 2.4 `subprocess`, `decimal`, `collections` Skipped themes ============== - Sorting order - Unicode and strings - File and OS interface - Other library modules