Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 24375

Real Python: Python 3.11 Preview: Task and Exception Groups

$
0
0

Python 3.11 will be released in October 2022. Even though October is still months away, you can already preview some of the upcoming features, including the new task and exception groups that Python 3.11 has to offer. Task groups let you organize your asynchronous code better, while exception groups can collect several errors happening at the same time and let you handle them in a straightforward manner.

In this tutorial, you’ll:

  • Install Python 3.11 alpha on your computer, next to your current Python installations
  • Explore how exception groups can organize several unrelated errors
  • Filter exception groups with except* and handle different types of errors
  • Use task groups to set up your asynchronous code
  • Test smaller improvements in Python 3.11, including exception notes and a new internal representation of exceptions

There are many other improvements and features coming in Python 3.11. Check out what’s new in the changelog for an up-to-date list.

Free Bonus:5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

Python 3.11 Alpha

A new version of Python is released in October each year. The code is developed and tested over a seventeen-month period before the release date. New features are implemented during the alpha phase, which lasts until May, about five months before the final release.

About once a month during the alpha phase, Python’s core developers release a new alpha version to show off the new features, test them, and get early feedback. Currently, the latest alpha version of Python 3.11 is 3.11.0a7, released on April 5, 2022.

Note: This tutorial uses the seventh alpha version of Python 3.11. You might experience small differences if you use a later version. In particular, a few aspects of the task group implementation are still being discussed. However, you can expect most of what you learn here to stay the same through the alpha and beta phases and in the final release of Python 3.11.

The first beta release of Python 3.11 is just around the corner, planned for May 6, 2022. Typically, no new features are added during the beta phase. Instead, the time between the feature freeze and the release date is used to test and solidify the code.

Cool New Features

Some of the currently announced highlights of Python 3.11 include:

  • Exception groups, which will allow programs to raise and handle multiple exceptions at the same time
  • Task groups, to improve how you run asynchronous code
  • Enhanced error messages, which will help you more effectively debug your code
  • Optimizations, promising to make Python 3.11 significantly faster than previous versions
  • Static typing improvements, which will let you annotate your code more precisely

There’s a lot to look forward to in Python 3.11! You can already read about the enhanced error messages in Python 3.11 Preview: Even Better Error Messages.

In this tutorial, you’ll focus on how exception groups can handle multiple unrelated exceptions at once and how this feature paves the way for task groups, which make concurrent programming in Python more convenient. You’ll also get a peek at some of the other, smaller features that’ll be shipping with Python 3.11.

Installation

To play with the code examples in this tutorial, you’ll need to install a version of Python 3.11 onto your system. In this subsection, you’ll learn about a few different ways to do this: using Docker, using pyenv, or installing from source. Pick the one that works best for you and your system.

Note: Alpha versions are previews of upcoming features. While most features will work well, you shouldn’t depend on any Python 3.11 alpha version in production or anywhere else where potential bugs will have serious consequences.

If you have access to Docker on your system, then you can download the latest version of Python 3.11 by pulling and running the python:3.11-rc-slimDocker image:

$ docker pull python:3.11-rc-slim
Unable to find image 'python:3.11-rc-slim' locallylatest: Pulling from library/python[...]$ docker run -it --rm python:3.11-rc-slim

This drops you into a Python 3.11 REPL. Check out Run Python Versions in Docker for more information about working with Python through Docker, including how to run scripts.

The pyenv tool is great for managing different versions of Python on your system, and you can use it to install Python 3.11 alpha if you like. It comes with two different versions, one for Windows and one for Linux and macOS. Choose your platform with the switcher below:

On Windows, you can use pyenv-win. First update your pyenv installation:

PS> pyenvupdate:: [Info] ::  Mirror: https://www.python.org/ftp/python[...]

Doing an update ensures that you can install the latest version of Python. You could also update pyenv manually.

On Linux and macOS, you can use pyenv. First update your pyenv installation, using the pyenv-update plugin:

$ pyenv update
Updating /home/realpython/.pyenv...[...]

Doing an update ensures that you can install the latest version of Python. If you don’t want to use the update plugin, you can update pyenv manually.

Use pyenv install --list to check which versions of Python 3.11 are available. Then, install the latest one:

Read the full article at https://realpython.com/python311-exception-groups/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]


Viewing all articles
Browse latest Browse all 24375

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>