Python 3.11 is OUT!

Finally, Python 3.11 is out and I’m going to tell you what you need to know about this version, and what are the major changes in it that may help you in your Python projects!

Faster CPython

Many improvements and optimizations were made in this version. CPython 3.11 is roughly 25% faster than CPython 3.10. Also, depending on your project, your speedup can be between 10% and 60%. This improvement was made especially for faster runtime and startup.

Better Error Traceback (PEP 657)

Now with Python 3.11, you can find the exact point where an error occurs:

Traceback (most recent call last):
File "distance.py", line 11, in <module>
print(manhattan_distance(p1, p2))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "distance.py", line 6, in manhattan_distance
return abs(point_1.x - point_2.x) + abs(point_1.y - point_2.y)
^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'x'

Now the interpreter points to the exact expression that raised the error instead of the whole line. This will be very helpful for Python students and beginners.

New Library for Handling TOML Files (PEP 680)

Since TOML files are gaining so much popularity these days for being used to create build systems for Python projects, a new library was added to be able to parse TOML files easily:

import tomllib

with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)

Exception Groups (PEP 654)

Now, we can raise multiple unrelated exceptions at the same time using Exception Groups.

Example:

eg = ExceptionGroup(
"one",
[
TypeError(1),
ExceptionGroup(
"two",
[TypeError(2), ValueError(3)]
),
ExceptionGroup(
"three",
[OSError(4)]
)
]
)
raise eg

When we want to catch this, we can use “except*” as follows to match all the exceptions in the Exception Group:

try:
raise eg
except* ValueError as ve:
# do something
except* KeyError as ke:
# do something

Exceptions With Notes (PEP 678)

We can add custom notes to each exception so we can know exactly what the problem is when the exception is raised.

When defining an exception, we can do the following:

try:
# some code that throws an error
except Exception as e:
e.add_note("this is my custom exception")
raise

Self Type (PEP 673)

This type allows us to easily annotate methods that return an instance of their class:

Example:

class Person:
def __enter__(self) -> Self:
self.speak()
return self

LiteralString Type (PEP 675)

This type will allow us to indicate that a function parameter can be of any literal string type:

from typing import LiteralStringdef run_query(sql: LiteralString) -> None
...

I only stated the most important ones (in my opinion), but there are much more. You can read the entire change log by clicking on this link: https://docs.python.org/3.11/whatsnew/3.11.html

Happy coding y’all!!
K.B.

If you need Django work, feel free to hire me at https://business.fiverr.com/share/YW9pAz

--

--

Kaïss Bouali, PSPO™, ITIL™, Oracle Cloud Associate
Kaïss Bouali, PSPO™, ITIL™, Oracle Cloud Associate

Written by Kaïss Bouali, PSPO™, ITIL™, Oracle Cloud Associate

A Python Software Engineer and Passionate Entrepreneur with 20+ years of experience in web development.

No responses yet