What’s New In Python 3.11

Mitku Dubale
3 min readDec 4, 2022

--

Release: 3.11.0

Date: December 04, 2022

Python 3.11 has just been released, and developers are encouraged to try out this latest version on non-production code, both to verify that it works with your programs and to get an idea of whether your code will benefit from its performance enhancements, It is between 10–60% faster than Python 3.10. On average, we measured a 1.25x speedup on the standard benchmark suite..

Here’s a rundown of the most significant new features in Python 3.11 and what they mean for Python developers.

More Informative Error Tracebacks

Python is often recognized as a good beginner programming language, with its readable syntax and powerful data structures. A challenge for all, but especially those new to Python, is how to interpret the traceback that’s displayed when Python encounters an error.

In Python 3.10, Python’s error messages were greatly improved. Similarly, one of Python 3.11’s most anticipated features will also boost your developer experience. Decorative annotations are added to the tracebacks and can help you more quickly interpret an error message.

Nicer Syntax for Asynchronous Tasks

Support for asynchronous programming in Python has evolved over a long time. Foundations were laid in the Python 2 era with the addition of generators. The asyncio library was originally added in Python 3.4, and the async and await keywords followed suit in Python 3.5.

The development has continued in later releases, with many small improvements added to Python’s asynchronous capabilities. In Python 3.11, you can use task groups, which provide a cleaner syntax for running and monitoring asynchronous tasks.

Exception improvements

Exceptions, Python’s error-handling mechanism, have received many new features in Python 3.11:

  • Multiple exceptions can be raised and handled at once with the new except* syntax and the new ExceptionGroup exception type. This allows the elegant handling of issues where multiple errors can be raised together, such as when dealing with asynchronous or concurrent methods or when dealing with multiple failures when retrying an operation.
  • “Zero-cost” exceptions: Exceptions now have no cost to a program unless they are actually raised. This means the default path for a try/except block is faster and uses less memory.
  • The time needed to catch an exception has been reduced by around 10%.
  • Exceptions can be enriched with contextual notes, separate from the text of the exception itself.

Typing improvements

Python’s type-hinting features make larger codebases easier to manage and analyze, and have increased significantly with each revision since Python 3.5. Python 3.11 brings in several new type-hinting additions.

Speed improvements

Many individual performance improvements landed in Python 3.11, but the single biggest addition is the specializing adaptive interpreter. Since an object’s type rarely changes, the interpreter now attempts to analyze running code and replace general bytecodes with type-specific ones. For instance, binary operations (add, subtract, etc.) can be replaced with specialized versions for integers, floats, and strings.

Python function calls also require less overhead in Python 3.11. Stack frames for function calls now use less memory and are more efficiently designed. Also, while recursive calls aren’t tail-optimized (which probably isn’t possible in Python, anyway), they are more efficient than in previous versions. The Python interpreter itself also starts faster, and core modules needed for the Python runtime are stored and loaded more efficiently.

Removing ‘dead batteries’ from the standard library

PEP 594 kicked off an effort to remove many so-called dead batteries, or modules that are obsolete or unmaintained, from the Python standard library. As of Python 3.11, those libraries are marked as deprecated but not yet removed; they will be removed entirely in Python 3.13.

--

--

Mitku Dubale

Software Developer | Django | Flutter | Python | REST API