Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the latest features of the second alpha version of Python3.10

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "what are the latest features of the second alpha version of Python3.10". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the latest features of the second alpha version of Python3.10.

Python3.9 has just been released, and the second alpha version of Python3.10 was released in early November. Through this version, we may be able to catch a glimpse of the future changes of Python.

The new features of the second alpha version of Python3.10 include the following three main parts:

Type annotation extension

Why type annotations are important

New methods and behaviors

Type annotation extension

The Python3.9 version thoroughly modifies and cleans up type hints and comments. The Python3.10 version seems to continue this trend, and the Python3.10 alpha 2 version extends the type annotation feature.

Changes in type annotations from Python 3.0to Python 3.10.

Delayed evaluation of type comments

The evaluation of type comments is always performed when the function is defined, which means that type comments are evaluated line by line in a top-down manner. This may seem logical, but there are two problems:

A type hint that references an undefined type (forward reference) is not valid and must be expressed as a string. For example, it should be "int" instead of "int" (although this only applies to custom types, not built / predefined types).

The speed of module import slows down due to the need to perform type prompts.

Therefore, the comments will be stored in _ _ annotations__, and then evaluated centrally, allowing forward references and performing module imports first (to reduce initialization time).

Union operator type

Python 3.10 introduces the | operator. You can use | as an OR when annotating data types. For example, there is a variable expected to be int or float, which we can write as int | float:

In versions prior to 3.10, the equivalent operator was written using the type.Union method, such as Union [int, float].

TypeAlias comment

Going back to the forward reference problem, a common solution to avoid forward references is to write them as strings.

However, writing types as strings can cause problems when assigning those types to variables, because Python assumes that the string literal type comment is just a string.

Using a type annotation variable where a type annotation is used will return an error. For example:

We are trying to use MyType as the type alias (alias), but MyType will be read as a string value instead of a type alias.

This is valid as long as ClassName is defined later in the code. Currently, this will cause an annotation error.

To solve this problem, this version adds a method that explicitly recognizes MyType as a type alias:

Why type annotations are important

The power of Python is that it is easy to use and master, and one of the reasons is that we don't need to define types explicitly throughout the code.

This may seem counterintuitive, but allowing developers to define types can greatly enhance the readability and maintainability of the code base. For example, extract the following from the source code of the transformers library:

Even if there is no context, we can read the code and know what data should be provided to these functions, classes, and methods, and which data types should be returned.

In complex code bases (or even simple ones), type comments can greatly improve readability. At the same time, not every developer wants (or needs) to use type annotations, so optional, exception-free features can achieve a perfect balance.

New method and implementation

In addition to the changes in type annotations, some updates have been made to other core features in version 3.10 alpha 2.

Add equal length tags to Zip

The first is PEP 618, which adds an optional strict tag to the zip () function. If you set strict = True, an error will be raised if the two inputs of zip are of different lengths.

There is no strict=True tag on the left, no error is raised, and the longer list is truncated to create a compression generator. If you set strict = True, an error will be raised.

Bit count of integers

Also known as "population count". This new method allows you to count the number of ones in the integer binary representation by simply writing int.bit_count ():

Dictionary view mapping

The three dictionary methods dict.keys (), dict.values (), and dict.items () return different views of the dictionary. Now, add the mapping attribute to each view object.

This new property is the types.MappingProxyType object, which wraps the original dictionary. If it is called on the view, the original dictionary is returned.

At this point, I believe you have a deeper understanding of "what are the latest features of the second alpha version of Python3.10?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report