#1
Release 3.11.2
Date February 28, 2023
Editor Pablo Galindo Salgado

This article explains the new features in Python 3.11, compared to 3.10.

Summary – Release highlights
  • Python 3.11 is between 10-60% faster than Python 3.10. On average, we measured a 1.25x speedup on the standard benchmark suite.
New syntax features: New built-in features: New standard library modules:
  • PEP 680: tomllib— Support for parsing TOML in the Standard Library
Interpreter improvements: New typing features: Important deprecations, removals and restrictions: New Features

PEP 657: Fine-grained error locations in tracebacks

When printing tracebacks, the interpreter will now point to the exact expression that caused the error, instead of just the line. For example:

Traceback (most recent call last):
  File "distance.py", line 11, in <module>
    print(manhattan_distance(p1, p2))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "distance.py", line 6, in manhattan_distance
    return abs(point_1.x - point_2.x) + abs(point_1.y - point_2.y)
                         
AttributeError: 'NoneType' object has no attribute 'x'


Previous versions of the interpreter would point to just the line, making it ambiguous which object was None
. These enhanced errors can also be helpful when dealing with deeply nested dict
objects and multiple function calls:

Traceback (most recent call last):
  File "query.py", line 37, in <module>
    magic_arithmetic('foo')
  File "query.py", line 18, in magic_arithmetic
    return add_counts(x) / 25
          ^^^^^^^^^^^^^
  File "query.py", line 24, in add_counts
    return 25 + query_user(user1) + query_user(user2)
                ^^^^^^^^^^^^^^^^^
  File "query.py", line 32, in query_user
    return 1 + query_count(db, response['a']['b']['c']['user'], retry=True)
                              ~~~~~~~~~~~~~~~~~~^^^^^
TypeError: 'NoneType' object is not subscriptable


As well as complex arithmetic expressions:

Traceback (most recent call last):
  File "calculation.py", line 54, in <module>
    result = (x / y / z) * (a / b / c)
              ~~~~~~^~~
ZeroDivisionError: division by zero


Additionally, the information used by the enhanced traceback feature is made available via a general API, that can be used to correlate bytecode instructions with source code location. This information can be retrieved using:
  • The codeobject.co_positions() method in Python.
  • The PyCode_Addr2Location()function in the C API.