The official version of Python 3.11 is here - let's see what's new

Finally, the official version of Python 3.11 is released!

On January 1, 2020, Python officially ended the maintenance of Python 2, which means that Python 2 has been completely retired and entered the era of Python 3. Since entering version 3, Python has officially released many modified branches, and now comes the latest version, Python 3.11.

In fact, there is an undisclosed secret in the research community, that is, Python does not run fast but is easy to use, so there are a lot of users, and Python has repeatedly ranked first in many most popular language lists. Many developers are looking forward to the improvement of the performance of this language, and some people wonder whether Python 4 will come at an inadvertent moment. Those who have this idea can put it aside. The father of Python, Van Rossum, said , Python 4.0 may not be coming.

Van Rossum once said: "The members of the Python core development team and I have no idea about Python 4.0 and are not interested. It is estimated that it will be numbered at least until 3.33. The acceleration of Python is gradual, and the 3.11 version will have new speed improvements. It is expected to be much faster than 3.10."

As Van Rossum said, according to official data, the latest release of Python 3.11 is 10-60% faster than Python 3.10 and is more user-friendly. After 17 months of development, this release is now publicly available.

The specific improvements in Python 3.11 are mainly reflected in: more detailed Error Tracebacks, faster code execution, better syntax for asynchronous tasks, improved type variables, support for TOML configuration parsing, and some other cool features (including quick start, Zero- Cost exception handling, exception groups, etc.).

According to Pablo Galindo Salgado, Python Steering Committee member and core developer, and Python3.10/3.11 release manager, a lot of work has gone into making 3.11 the best version of Python possible.

New features in Python 3.11

Error Tracebacks

Python is a beginner-friendly programming language with an easy-to-understand syntax and powerful data structures. But for those who are new to Python, there is a difficult problem, that is, how to interpret the traceback displayed when Python encounters an error.

Python 3.11 adds Decorative annotations to tracebacks to help users interpret error messages more quickly. To get this functionality, add the following code to the inverse.py file.

For example, you can use inverse() to calculate the inverse of a number. Since 0 has no countdown, an error is thrown when running the following code.

Note the ^ and ~ symbols embedded in the traceback, which point to the code that caused the error. As with the previous tracebacks, you should start at the bottom and work your way up. This kind of operation is very useful for finding errors, but if the code is too complex, annotated tracebacks are better.

faster code execution

Python is notoriously slow, for example a regular loop in Python is orders of magnitude slower than a similar loop in C.

Python officials are working on improving this defect. In fall 2020, Mark Shannon proposed several performance improvements in Python. This proposal is called the Shannon Plan (Shannon Plan), and they hope to increase the speed of Python by 5 times through several version updates. Microsoft officially joined the initiative soon after, and the company is supporting developers, including Mark Shannon and Guido van Rossum, working on the "Faster CPython" project.

An important proposal in the "Faster CPython" project is PEP 659, based on which, Python 3.11 has many improvements.

PEP 659 describes a "specializing adaptive interpreter". The main idea is to speed up the code by optimizing frequently performed operations, which is similar to JIT (just-in-time) compilation. It's just that it doesn't affect compilation, instead, Python's bytecode is dynamically adjusted or changeable.

The researchers added a new step in bytecode generation called "quickening," which allows instructions to be optimized at runtime and replace them with adaptive instructions.

Once the function has been called a certain number of times, the quickening instruction kicks in. In CPython 3.11, quickening starts after eight calls. You can observe how the interpreter adapts to the bytecode by calling dis() with the adaptive parameter.

In benchmarks, CPython 3.11 is on average 25% faster than CPython 3.10. The Faster CPython project is an ongoing project, and there are already several optimizations scheduled for release with Python 3.12 in October 2023. You can follow the project on GitHub.

Project address: https://github.com/faster-cpython/ideas

Better syntax for async tasks

Support for asynchronous programming in Python has evolved over a long time. Generators were added in the Python 2 era, the asyncio library was first added in Python 3.4, and the async and await keywords were added in Python 3.5. In Python 3.11, you can use task groups, which provide a more concise syntax for running and monitoring asynchronous tasks.

Improved type variables

Python is a dynamically typed language, but it supports static typing through optional type hints. The foundation of Python's static type system is defined in PEP 484 from 2015. Every Python version since Python 3.5 has introduced several new proposals related to types.

Python 3.11 released a new high of 5 type-related PEPs:

  • PEP 646: Mutable Generics

  • PEP 655: Mark Individual TypedDict Items as Required or Likely Missing

  • PEP 673: The Self Type

  • PEP 675: Arbitrary Literal String Types

  • PEP 681: Data Class Conversion

Support TOML configuration parsing

TOML is an acronym for Tom's Obvious Minimal Language. This is a configuration file format that has become popular over the past decade. The Python community has adopted TOML as the preferred format when specifying metadata for packages and projects.

Although TOML has been used for many years, Python does not have built-in support for TOML. Things changed in Python 3.11 when tomllib was added to the standard library. This new module builds on top of the toml third-party library and allows parsing of TOML files.

Here is an example TOML file named units.toml:

Other functions

In addition to the above major updates and improvements, Python 3.11 has more features worth exploring, such as faster program startup speed, more changes to exceptions, and small improvements to string formatting.

Faster program startup

A big result of the Faster CPython project is faster startup times. When you run a Python script, the interpreter needs a few things to initialize. This causes even the simplest programs to take milliseconds to run.

In many cases, the time required to start a program is negligible compared to the time required to run the code. But in short-running scripts, such as typical command-line applications, startup time can significantly affect program performance. Consider, for example, the following script, which was inspired by the classic cowsay program.

In snakesay.py, you read a message from the command line and print it in a speech bubble with a cute snake on it. You can make a snake say anything. Here's a basic example of a command-line application that runs pretty fast, but still takes a few milliseconds. A large part of this overhead occurs when Python imports modules.

You can use the -X importtime option to display the time taken to import a module. The numbers in the table are in microseconds and the last column is the format of the module name.

The example was run on Python 3.11 and 3.10 respectively, and the results are shown in the figure below. Python 3.11 imports faster, which helps Python programs start faster.

zero cost exception

The internal representation of exceptions is different in Python 3.11. Exception objects are more lightweight, and exception handling has changed. So as long as the except clause is not triggered, the overhead in the try ... except block is less.

So-called zero-cost exceptions were inspired by other languages ​​such as C++ and Java. When your source code is compiled to bytecode, the compiler creates jump tables, thereby enabling zero-cost exceptions. If an exception is raised, these jump tables are queried. If there are no exceptions, the code inside the try block has no runtime overhead.

exception group

Previously, you learned about task groups and how they handle multiple errors at the same time. This is all thanks to a new feature called exception groups.

We can think of exception groups as regular exceptions that wrap several other regular exceptions. While exception groups behave like regular exceptions in many ways, they also support special syntax that helps you handle each wrapped exception efficiently. You can create an exception group by giving a description and listing the wrapped exceptions as shown below.

Abnormal Notes

Regular exceptions have the extensibility of adding arbitrary notes. You can add a note to any exception with .add_note() and view existing notes by inspecting the .__notes__ attribute.

negative zero formatting

When doing calculations with floating-point numbers, you may encounter a strange concept-negative zero. You can observe that negative zero and regular zero are rendered differently in the REPL, as shown below.

See the original documentation for more details on the updates for Python 3.11.

Guess you like

Origin blog.csdn.net/chen565884393/article/details/127533311