CodeX
Published in

CodeX

Python 3.11: Unpacking Five New Features.

Photo by SHVETS production: https://www.pexels.com/photo/young-relocating-lady-packing-carton-boxes-sitting-on-floor-7203812/

Python 3.11 is almost here, and every new release comes with new features. In this article, we are going to look at five(5) cool new features of Python 3.11 that you should look forward to.

1. Up to 60% faster than the previous version

Python 3.11 is expected to be up to 60% faster than Python 3.10, depending on your workload. On average, the expectation is that it will be 25% faster than previous versions in terms of startup and runtime. To confirm this, I ran the same code with Python 3.11 and Python 3.10 and compared the execution times. The runtime results confirm that Python 3.11 is much faster than previous versions. See the codes below:

Using Python 3.10

The execution time is 5.50 secs.

Using Python 3.11

The execution time is 2.14 secs.

2. Improved error locations in the tracebacks

To demonstrate this new feature, we are going to use two examples. We will run the first example using Python 3.10, and the second example using Python 3.11. Both codes will generate an error. Take note of the difference in error messages.

Example 2

Both codes above are outputting a zero division error. The first example only returns the line with the error (c = a/b) but it does not specify what exactly is causing the error. In the second example (using Python 3.11), not only does it return the line with the error, but it also points to the actual element in the code causing the error, the division(/) sign.

3. Annotate code with self type

In previous Python versions, if you wanted to annotate methods that return the instance of the class, you would use the TypeVar class of the typing module. Python 3.11 has added a new intuitive way to annotate methods without using the TypeVar-based approach. The first code below demonstrates how the TypeVar-based approach is implemented. The second code demonstrates the new Python 3.11 approach using the self type.

With Python 3.11, you will annotate a method of a class using the Self type. You can see below that the Python 3.11 approach makes code more concise and easy to understand.

4. Handle multiple exceptions with Exception Group

Python 3.11 will include a new built-in exception type called ExceptionGroup. What is exciting about this exception type is that it will make it possible to raise multiple different exceptions or errors at the same time. The ExceptionGroup takes two arguments, a string, then followed by a sequence of errors that we want to raise and handle. See the code below:

Once we raise the errors, we can use the new except* blocks to handle the errors. See below:

The except* will also make it possible to group multiple exceptions. For more insight on this new built-in standard check the link below:

5. TypeDict — Required and Not Required Keys.

Python 3.8 introduced and added TypedDict to the typing module. TypedDict type made it possible to create a dictionary with specific keys and values. However, if we wanted certain information to be optional in the dictionary, it was not easy to implement with TypedDict. Let’s use an example to demonstrate this. We are going to create a dictionary with three(3) keys — name, age, and car. See below:

The dictionary above will require, the name, the age, and the car the person drives. However, we want to make the car information optional, because not everyone has a car. To implement that in Python 3.10 or older versions, we have to create another class (child class) that will implement the optional key. That was the only way to do it in previous Python versions. Here is how the code will look:

In the above code, the total is false because this car key is now optional. If someone has not got a car, they can just leave out the car part.

Python 3.11 has introduced Required and NotRequired types to get around this type of problem. Using these types (Required and NotRequired), we do not have to create a child class to implement an optional key, instead, we can just annotate the optional key as NotRequired. See below:

Conclusion.

These are some of the new features that will come with Python 3.11. The beta preview of Python 3.11 was released on, May 8, 2022. The final version of Python is expected to be released in October 2022. Please share the article, follow me, and subscribe to get a notification when I publish an article.

--

--

--

Everything connected with Tech & Code. Follow to join our 900K+ monthly readers

Recommended from Medium

Keeping your head sane in your way to web development mastery.

RECAP GENI Quiz №3

Intro the Class Inheritance

Building a Custom Power BI Visual from scratch (KPI Card)

Highs and lows of exploring API’s

CQRS + Event Sourcing (Making SharePoint Decent Again, Maybe)

Tutorial: Translating your Intercom inbox with Stamplay and Google Translate

Coding Problem: Validate Subsequence in Go

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Benjamin Bennett Alexander

Benjamin Bennett Alexander

Tech ( Python, Data science and Blockchain) and Finance enthusiast. I just share stories.

More from Medium

Python fastest way to iterate a list

What is SQLAlchemy?

Dockerize Python application

How to Package Your Python Code