What Is New in Python 3.1
Python 3.1 was released on June 26, 2009, as a feature update to Python 3.0. The most developer-visible addition is collections.OrderedDict, which guarantees FIFO dict ordering before it became the default in 3.7. Python 3.1 also introduced the comma-separator format for numbers, improved the io module, and brought significant performance improvements across the interpreter.
| Category | Change | PEP / Reference |
|---|---|---|
| Standard Library | collections.OrderedDict -- insertion-ordered dictionary |
PEP 372 |
| Standard Library | Comma separator in format strings ("{:,}".format(1234567)) |
PEP 378 |
| Standard Library | importlib -- Python implementation of the import system |
PEP 302 |
| Standard Library | io.BytesIO and io.StringIO performance improvements |
-- |
| Performance | Significant speedups in I/O, float formatting, and many core operations | -- |
| Standard Library | unittest test discovery, skip decorators, assertIs, assertIn |
-- |
| Standard Library | logging.NullHandler for library authors |
-- |
| Standard Library | Faster int to string conversion |
-- |
collections.OrderedDict (PEP 372)
OrderedDict is a dict subclass that maintains entries in insertion order. Before plain dicts guaranteed ordering in 3.7, OrderedDict was the standard tool for order-dependent dict operations like LRU caches, config serialization, and ordered counters.
from collections import OrderedDict
od = OrderedDict()
od["first"] = 1
od["second"] = 2
od["third"] = 3
list(od.keys()) # ["first", "second", "third"] -- guaranteed order
# Move to end
od.move_to_end("first") # ["second", "third", "first"]
OrderedDict also supports equality comparison that considers order -- two OrderedDicts are equal only if both their contents and their insertion order match. This differs from plain dict equality.
Number Formatting with Commas (PEP 378)
The format specification mini-language gained a comma separator for numbers:
print(f"{1234567:,}") # 1,234,567
print(f"{3.14159:,.2f}") # 3.14
print(f"{10**9:,}") # 1,000,000,000
unittest Improvements
Python 3.1 significantly upgraded unittest. New features include test discovery (run python -m unittest discover to find test files automatically), skip decorators (@unittest.skip, @unittest.skipIf), and new assertion methods: assertIs(), assertIn(), assertIsNone(), assertRaises() as context manager, and assertAlmostEqual() with configurable places.
FAQ
Is OrderedDict still useful after Python 3.7 made dict order guaranteed?
For most cases, no -- a plain dict maintains insertion order and is faster. OrderedDict is still useful when you need order-aware equality (od1 == od2 considers order), move_to_end(), or explicit signaling of order-dependence in an API.
What did Python 3.1 improve in terms of float formatting?
Python 3.1 adopted a smarter float-to-string algorithm (David Gay's dtoa) that produces the shortest decimal representation that round-trips back to the same float. For example, str(0.1) now gives '0.1' instead of '0.10000000000000001'.
What is the purpose of logging.NullHandler?
Library authors should add logging.NullHandler() to their loggers to prevent "No handlers could be found for logger X" warnings when their library is used in an application that hasn't configured logging. The application then controls where log output goes; the library just emits records.
When was Python 3.1 end-of-life?
Python 3.1 reached end-of-life on April 9, 2012. It should not be used for any new projects. The 3.x series that remains relevant starts at 3.8 (security fixes until October 2024) and newer.
Does unittest discovery require any configuration to find test files?
By default, python -m unittest discover looks for files matching test*.py starting from the current directory. You can override the start directory (-s), file pattern (-p), and top-level directory (-t). No configuration file is required.