Python 3.11: What’s New?

Python 3.11: What's New?

Python is an open-source programming language. What makes Python open-source is that it is constantly been upgraded by the Python community. The community comes up with exciting new upgrades and features in Python every year.

This is 2022 and this is the year of Python’s 3.11 version.
Python 3.11 has been officially launched on October 24.

New features and upgrades in Python 3.11

Python 3.11 logo

1. Python 3.11 is faster than Python 3.10

The community has well put in docs that Python 3.11 can be up to 60% faster than Python 3.10. Also, the benchmark suite runs up to 25% faster than in 3.10. This can be profitable to Python programmers and data scientists.
To run this benchmark test to compare the speeds of Python 3.10 and Python 3.11, you will need a docker installation on the system.

2. Exception Notes

Python 3.11 offers a new function called add_notes() to make the exceptions as expressive as possible. Any custom messages can be passed so that they will be displayed if any error will be raised.

Look at this code snippet:

add_note() to exceptions in python

On running, you can see your custom note has been displayed on the terminal

error message

This feature is pretty useful in dealing with errors.

Talking about the error part, the next major upgrade Python 3.11 came with is explained in the next part.

3. More Expressive Error Messages

Python 3.11 has a new upgrade with the display of error messages. Previously, the interpreter would throw an error mentioning the line number and error description, but with this version, the ~ and ^ characters are added to specify where the error actually might be.

Take a previous example, Division by zero error.

Python 3.11 Python 3.11: What's New? This is 2022 and this is the year of Python's 3.11 version.Python 3.11 has been officially launched on October 24.

The characters ~ and ^ stating the actual part of the error.

This feature makes debugging a lot easier.

4. New Modules in Python 3.11

a. TOML Support

The human-friendly and computer-friendly Tom’s Obvious Minimal Language has now been included in Python’s standard library tomllib. TOML provides metadata for projects and packages.

b. wsgiref.types

This module provides utilities for static type checking.

5. Upgradations to Standard Libraries

Most of the common and stand libraries of Python gone improved with Python 3.11.

  • math library has been updated with functions for computing cube root and power of 2.
  • datetime library contains functions for iso formats
  • A new module fractions has also been added that let user can create fractions from strings.
  • Libraries such as typing, os, functools, enum, SQLite, etc are upgraded with new features.

6. typing : self

In previous versions of Python, the function returning self-objects was a lot more confusing and almost impossible. But typing library in Python 3.11 includes a self that lets you return the instances of a class.

7. Typedict: Required and NotRequired

All the fields in the typedict used to be compulsory. But Python 3.11 introduces Required and NotRequired, which can mark if the item is mandatory or not.

class Movie(TypedDict):
   title: str
   year: NotRequired[int]

The title is a required parameter whereas the year is not.
Considering the examples,

m1: Movie = {"title": "Black Panther", "year": 2018}  
m2: Movie = {"title": "Star Wars"}
m3: Movie = {"year": 2022} 

object m1 and m2 will work fine but object m3 will throw an error.

Conclusion

These were some major upgrades in Python 3.11.

It is recommended to update Python with the latest version because 3.11 is faster, more expressive, and has static typing support, and TOML support.

For more detailed information about Python 3.11, visit here.

Installation of Python 3.11:
Python 3.11 is available to install on the official websites.
Check it out here.

Thank-you.


Also Read:

Share:
Avatar of Ayush Purawr

Author: Ayush Purawr