What’s new since Python 2.2?

I learned Python when 2.2 was the new hotness. Since then I’ve picked up a lot of newer Python features but never really sat down to think of all the new language and library capabilities the system had. Here’s a list of some highlights in the 2.x releases I picked up off the what’s new pages. 2.6 (3.0) means the feature is available in 2.6 as a backport from 3.0. 2.6f (3.0) means the feature is only in 2.6 as a __future__.

Language

  • 2.3, 2.4, 2.5: Generators. Generator expressions. send(), throw(), close().
  • 2.4: Function and method decorators. 2.6 (3.0): Class decorators
  • 2.5: Conditional expressions (“foo” if x == 3 else “bar”)
  • 2.5: Absolute imports (default in 3.x)
  • 2.5: try/except/finally unified
  • 2.5, 2.6: with statement (this one is important; need to use it more)
  • 2.6 (3.0): except ExceptionClass as variableName:
  • 2.6 (3.0): strings are unicode. bytes type.
  • 2.7 (3.1): dictionary and set comprehensions

Libraries

  • ???: codecs.open() as a way to be explicit about character encoding
  • 2.3, 2.4: Sets. 2.7 (3.1): set literals
  • 2.3: Universal newline mode for files
  • 2.3: logging
  • 2.3: CSV files
  • 2.5: functools.partial() for closure-like partial function application
  • 2.5: ctypes, quick access to shared libraries
  • 2.5: ElementTree (xml.etree)
  • 2.5: sqlite3
  • 2.6f (3.0): str.format(). 2.7: autonumbering
  • 2.6f (3.0): print() as a function
  • 2.6 (3.0): abstract base classes, particularly for collections and numbers
  • 2.6: json (via simplejson)
  • 2.7: collections.OrderedDict
  • 2.7: argparse
  • 2.7 (3.1): dictionary views
  • 2.7: collections.Counter (replaces my accumulator.py)
  • 2.7: unittest improvements, backported to 2.4 as unittest2

Actually using some of these features is harder: Google AppEngine is stuck at 2.5 and Debian is stuck at 2.6. And of course 3.2 is the new hotness now, but largely not usable for ordinary user code.