Sobes.tech
Junior — Middle

What features and innovations were introduced in Python 3.11?

sobes.tech AI

Answer from AI

Python 3.11 introduced several important new features and improvements:

  • Performance improvements: thanks to interpreter optimizations, Python 3.11 runs approximately 10-60% faster than 3.10.
  • Precise error messages: enhanced exception diagnostics with exact error location and context.
  • New features in typing:
    • Self for method annotations returning an instance of the class.
    • typing.ParamSpecArgs and typing.ParamSpecKwargs for more flexible annotations.
  • Exception Groups and except*: support for handling multiple exceptions simultaneously, useful for asynchronous code.

Example of using Exception Groups:

try:
    raise ExceptionGroup("group", [ValueError("val"), TypeError("type")])
except* ValueError as e:
    print(f"Handled ValueError: {e}")

These changes make Python 3.11 faster and more convenient for debugging and writing typed code.