logo

Get in touch

Awesome Image Awesome Image

Python Development April 24, 2023

What’s New In Python 3.11 & Python 3.11.3 ?

Writen by Dharmesh Patel

5,988

The Python 3.11 changelog is a seemingly unending collection of bug fixes, improvements, and additions, the majority of which you may never discover. However, a few crucial new features may significantly improve your Python process when the stable release arrives. 

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

1. Increased Speed

The first big change that will thrill data scientists is an increase in speed—the usual benchmark suite now runs around 25% faster than in 3.10. According to the Python documentation, 3.11 can be up to 60% faster in some cases.

2. More Informative Error Tracebacks

Another immediately useful feature in Python 3.11 is more detailed error messages. Python 3.10 already had better error reporting, thanks to the new parser used in the interpreter. Now, Python 3.11 expands on that by improving error message that pinpoints the specific location of the fault. Rather than returning a 100-line traceback that ends with a difficult-to-understand error message, Python 3.11 points to the expression that produced the error. 

Traceback

In the preceding example, the Python interpreter refers to the x that caused the script to fail due to its None value. Because there are two objects with the ‘x’ attribute, the error would have been unclear in current Python versions. However, 3.11’s error handling clearly identifies the faulty expression. 

This example shows how 3.11 can identify an issue in a deeply nested dictionary and clearly identify the key the error belongs to. 

Read more: Everything you need to know 3.11

3. Exception Notes

“Explicit is better than implicit.”

The preceding text is the second line of the Zen of Python, which is a list of Python’s 20 design principles. This is an example of the rule that Python code should be as expressive as feasible.

Python 3.11 includes exception notes to emphasise this design approach. (PEP 678). When you raise an error, you may now call the add_note() function inside your unless clauses and give a custom message. 

Import Math

If you have written a custom exception class like below, you can add multiple notes to the class inside the protected class-level attribute __notes__: 

List of strings

4. Exception Improvements

  • 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. 

5. New Typing Function: Self

Statically typed languages aid in the readability and debuggability of your code. Defining the exact kind of variables, function inputs, and outputs can save you hours of debugging effort and help others read your code more easily. Adding typing annotations will also allow modern IDEs to display function definitions as you enter their names, making your functions easier for others to understand.

Until now, Python’s strong typing module provided classes for almost any data type, except for classes that return instances of themselves. 

Import self Release date

However, Python 3.11 provides the Self class, which you can include in the declaration of a function if the function’s return value is self or a new instance of the class itself.

6. Upgrades to the Standard Libraries

There are a few other quality-of-life improvements to the standard libraries. First, two long-awaited functions are added to the math module. It’s remarkable that it took Python 28 years to include the cube-root function, but as the phrase goes, better late than never.  

A new feature in the fractions module allows you to build fractions from strings: 

Import fraction

As you can see, the fractions module is useful for some arithmetic operations. I particularly enjoy how you can get the fraction that is closest to the supplied float. You can go one step farther by specifying a denominator limit: 

Fraction import limit

There is also a new tomllib module for parsing TOML documents. Tom’s Obvious Minimal Language (TOML) is a common file format for creating human-readable configuration files.

Python uses TOML, or Tom’s Obvious Minimal Language, as a configuration format (as in pyproject.toml), but doesn’t expose the ability to read TOML-format files as a standard library module. Python 3.11 adds tomllib to address that problem. 

Note that tomllib doesn’t create or write TOML files; for that you need a third-party module like Tomli-W or TOML Kit

Import Tomllib

7. Removing ‘dead batteries’ from the standard library

One of Python’s strengths in its early days was that it came with batteries included. This somewhat mythical phrase is used to point out that a lot of functionality is included in the programming language itself. 

However, the real batteries are available in Python’s standard library. This is a collection of packages that come included with every installation of Python.

In total, the standard library consists of several hundred modules: 

Import SYS

You can see which modules are available in the standard library by inspecting sys.stdlib_module_names. 

Over time, the usefulness of the standard library has diminished, primarily because distribution and installation of third-party modules have gotten much more convenient. Many of Python’s most popular features now live outside the main distribution. Data science libraries like NumPy and pandas, visualization tools like Matplotlib and Bokeh, and web frameworks like Django and Flask are all developed independently. 

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. 

8. Conclusion

A new release of Python is always cause for celebration, and acknowledgment of all the effort that’s been poured into the language by volunteers from around the world.

In this blog, you’ve seen new features and improvements like:

    • Better error messages with more informative tracebacks
    • Faster code execution due to considerable effort in the Faster CPython project
    • Task and exception groups that simplify working with asynchronous code
  • Several new typing features that improve Python’s static typing support
  • Native TOML support for working with configuration files

hire python developer

Bringing Software Development Expertise to Every
Corner of the World

United States

India

Germany

United Kingdom

Canada

Singapore

Australia

New Zealand

Dubai

Qatar

Kuwait

Finland

Brazil

Netherlands

Ireland

Japan

Kenya

South Africa