ElectronicsGadgetITScienceTechnology

Top new features and fixes in Python 3.11

The Python programming language releases new versions every year. A feature-locked beta release will be released in the first half of the year and a final release towards the end of the year.

Python 3.11 has just been released. Developers are encouraged to try this latest version on non-production code to confirm that it works in their programs and to determine if their code would benefit from the performance improvements.

Here’s an overview of the most important new features in Python 3.11 and what they mean for Python developers.

Increased speed

Python 3.11 has many individual performance improvements, but the biggest additions are: Specialized in adaptive interpretingSince object types rarely change, the interpreter analyzes the running code and tries to replace common bytecodes with type-specific ones. For example, binary operations (addition, subtraction, etc.) can be replaced with specialized versions for integers, floats, and strings.

Python 3.11 also requires less overhead for Python function calls. Function call stack frames now use less memory and are designed more efficiently. Also, recursive calls are not tail-optimized (probably not possible in Python), but are more efficient than previous versions. The Python interpreter itself also starts faster and stores and loads the core modules required by the Python runtime more efficiently.

According to the official Python benchmark suite, Python 3.11 runs about 1.25 times faster than Python 3.10.This speedup is totalling measurement. Some run much faster, while many others run slightly faster or about the same speed. Still, the best part about these improvements is that they come for free. You don’t need to change the code of your Python programs to take advantage of the Python 3.11 speedups.

Enhanced error information

Another immediately useful feature of Python 3.11 is more detailed error messages. Python 3.10 already has Improved error reporting, thanks to the new parser used by the interpreter. Python 3.11 now extends it by providing detailed feedback that a particular part of a given expression caused an error.

Consider the following code which throws an error:

x = [1,2,3]
z = x[1][0]

In Python 3.10 I get the following error message, which is not very helpful:

  File "C:Python311code.py", line 2, in <module>
    z = x[1][0]
TypeError: 'int' object is not subscriptable

Rather than leave us wondering, int is not scriptable, so the error trace for Python 3.11 is just Part of the line that produces the error:

  File "C:Python311code.py", line 2, in <module>
    z = x[1][0]
        ~~~~^^^
TypeError: 'int' object is not subscriptable

Well, there is no ambiguity as to where the problem lies.

Improved exceptions

Exceptions, Python’s error handling mechanism, received a number of new features in Python 3.11.

  • Can raise and handle multiple exceptions at once new except* syntax and new ExceptionGroup Exception type. This allows you to elegantly handle issues where multiple errors can occur together, such as when dealing with asynchronous or concurrent methods, or when handling multiple errors when retrying an operation.
  • “Zero Cost” Exceptions: exceptions no longer cost your program unless they actually raise an exception. this is, try/except Blocks are faster and use less memory.
  • The time required to catch the exception is Approximately 10% reduction.
  • Exceptions are: Enhanced with context notesapart from the text of the exception itself.

Improved input

Python’s type hinting feature makes it easier to manage and analyze larger codebases, and has been greatly enhanced with each revision since Python 3.5. Python 3.11 added some new type hints.

self type

class method return self In the past, obtuse and verbose annotations had to be useful. Typing Self You can simply annotate the return value of your class method like this: SelfUseful and predictable results are obtained from the analytical tools of such methods.

any string literal type

Previously, type annotations had no way of indicating that a given variable should be a string literal (that is, a string defined in source code).new Typing.LiteralString annotation Fix it. A new annotation allows linters to test whether a variable is a string defined in the source or a new string consisting of: that’s all Source definition string.

data class conversion

Since Python 3.7, data classes make it easy to define classes that follow a common pattern for creating properties based on initialization parameters. But there was no standard mechanism to allow it. behaved Like data classes (but they weren’t data classes themselves), use type annotations to declare behavior. data class conversion to add typing.dataclass_transform A decorator that indicates how the specified function, class, or metaclass behaves like a data class.

variable-length generics

Original proposal for type hints include TypeVarhow to specify a generic function using a single parameterized type (e.g. type) T that is int or floatAdded Python 3.11 TypeVarTuple, or “variable-length generics”. This can be used to specify placeholders for not just one type, but a set of types represented as a tuple. This is especially useful in libraries such as NumPy that can pre-check for errors such as whether the provided array is of the correct shape.

TOML read-only support in stdlib

Python uses TOML (Tom’s Obvious Minimal Language) as its configuration format ( pyproject.toml), but the ability to read TOML format files is not exposed as a standard library module. Added Python 3.11 tomllib to address that issue.Please note tomllib won’t create Also write TOML file; for that you need a 3rd party module like Tomry-W Also TOML kit.

Atomic grouping and acceleration of regular expressions

Python’s re The module for working with regular expressions lacked some features found in other implementations of regular expressions.one atomic group, is widely supported in other languages. Python 3.11 now supports this pattern using General syntax for atomic groups (As an example, (?>...)).

of re The module’s pattern matching engine has also been slightly rewritten and runs about 10% faster.

Remove “dead battery” from standard library

PEP 594 many so-called dead battery, or obsolete or unmaintained modules from the Python standard library. As of Python 3.11, these libraries are marked as deprecated, but have not yet been removed. They will be completely removed in Python 3.13.

Other Python 3.11 additions, fixes and changes

Python 3.11 also includes many more minor improvements.

  • Python objects require less memory because namespaces are created lazily and namespace dictionaries share keys wherever possible.
  • A dictionary whose keys are all Unicode eliminates the need to store hashes, which reduces dictionary size and improves cache efficiency.
  • CPython runtime, reference interpreter for Python, Compilation to WebAssembly now has experimental supportThis could be useful for future development of projects such as PyScriptThis allows Wasm-compiled Python runtimes to work in the browser.

Copyright © 2022 IDG Communications, Inc.

https://www.infoworld.com/article/3660550/the-best-new-features-and-fixes-in-python-311.html#tk.rss_all Top new features and fixes in Python 3.11

Show More
Back to top button