Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Cool New Features in Python 3.11 (Summary)

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 all around the world.

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

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

You may not be able to take advantage of all the features right away. Still, you should strive to test your code on Python 3.11 to make sure your code is future ready. Do you notice any speed up? Share your experience in the comments below.

If you’d like to learn more about the concepts in this course, check out:

Download

Sample Code (.zip)

8.4 KB
Download

Course Slides (.pdf)

570.0 KB

00:00 In the previous lesson, I showed you some odds and ends in the release, like suppressing negative zeros. This is the summary lesson. I’ll discuss whether you should upgrade, summarize what I’ve gone through, and point you at some further material in case you’re interested. This course was all about the new things in Python 3.11.

00:19 There was a big focus on making Python faster and, following on to Python 3.10, continued improvement on error messages. In this case, the tracebacks got better.

00:30 This release saw the addition of TOML to the standard library, the creation of exception groups, and the addition of notes to those same exceptions. I showed you the new task creation mechanisms for asyncio and new additions to the typing module.

00:49 To be or not to be? That’s the question, closely followed up by to do or not to do? Wa diddy diddy dum diddy do. Personally, I’ve found the improved error messages in Python 3.10 made a big difference in my coding and debugging.

01:04 I suspect the improvements to tracebacks in 3.11 will have a similar impact. And of course, speed improvements are always welcome. Overall, If you’ve got a good test suite for your software, I think it’s worth trying 3.11, and if nothing breaks, you’re probably good. As always with upgrades, your degree of risk is related to what you’re running and how fragile your system is. If you’ve got good tests, just do it.

01:29 For more information on the release, you can see the What’s New doc, or if you want to know more about the PEG parser that was introduced in 3.9, which is now fully being taken advantage of, you can see PEP 617.

01:44 These three talks from a variety of core developers discuss the speed improvements if you want to know more about how they were accomplished.

01:53 If you want to take your traceback and error output to the next level, there are two third-party libraries that you might be interested in, The Better Exceptions Library and Friendly. Both help you give better error information to your users. To learn more about TOML, you can see their spec. If you want to use TOML before 3.11, you can use tomli or TOML Kit.

02:18 Note that both of these use a slightly different interface than the 3.11 tomllib, so you can’t just drop them in as replacements. TOML Kit as the added advantage of being able to write TOML as well as read it, and that includes style preservation.

02:35 If you’re looking for more Real Python content, this tutorial was part of the 3.11 preview series and goes into TOML in depth. Or if you want to learn more about asyncio, this tutorial and companion course may be of interest.

02:50 If you want to dig into asyncio as well as other parallel coding tools, this tutorial and course may be your thing. And finally, if you’d like to learn more about type hints in Python, this guide and course can point you along the way.

03:06 That’s it for what’s new in Python 3.11. I hope you found the course helpful. Thanks for your attention.

Avatar image for marcinszydlowski1984

marcinszydlowski1984 on Oct. 26, 2022

Hello Christopher,

can you talk more about the “Self” keyword or put a better example of it? The official documentation just mention this but there’s no advanced examples. Is it just a syntactic sugar? The same effect we can get using the __future__ built-in package:

from __future__ import annotations

...

class MyClass(SomeBaseClass):
    some_property = property(...)

    def some_method(self) -> MyClass:
        ...
        return self

    @staticmethod
    def some_method_2() -> MyClass:
        ...
        return MyClass(...)

...

m = MyClass()
m_2 = m.some_method()
m_2.[cursor here]       # Type hint will directly show the fields, classes and methods for the MyClass

This works for every type of method, no matter it is an instance method (built-in or custom), @classmethod or @staticmethod.

Avatar image for Geir Arne Hjelle

Geir Arne Hjelle RP Team on Oct. 26, 2022

Hi Marcin,

there are a bit more detail in the examples on realpython.com/python311-new-features/#improved-type-variables and realpython.com/python311-tomllib/#self-type.

As you note, you can use future annotations to add similiar hints without Self. This works well, as long as your class is not subclassed. Consider this continuation of your example:

class MySubclass(MyClass): ...

The return-type of MySubclass.some_method() is MyClass according to the annotation. However, the method is returning self which is a MySubclass object instead. Self will correctly handle this kinds of cases.

Avatar image for marcinszydlowski1984

marcinszydlowski1984 on Oct. 27, 2022

Thank you Geir Arne,

I missed the links which you provided but now it’s clear and I see the purpose. However, when I do inherited classes, I create overloaded methods which return proper types and I didn’t even realized that the problem may occur in other situations.

Become a Member to join the conversation.