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 features in Python 3.10

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the functions of Python 3.10". Interested friends may wish to have 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 functions of Python 3.10.

Highlight features in Python 3.10

(1) the frequency in the binary representation is 1

A new method, bit_count (), is introduced, which returns the number that exists in the binary representation of the integer. The result will be independent of the symbol of the integer. One use case for this feature is in information theory, where for two strings of equal length, you can find the total number of different positions of the two strings. This difference is called hamming distance (see Wiki). Read the history of this feature in Python here.

In the background, this method only calls the count method of strtype asstr.count ('1'). The following example illustrates this:

# Positive integer > > num = 108b1101100' > > bin (num) '0b1101100' > num.bit_count () 4 # Negative integer > num =-108 > bin (num)'-0b1101100' > num.bit_count () 4 # Under the hood > bin (num) .count ('1')

(2) the compression will be "strict"

The new optional keyword parameter strict is added to the zip function. If passstrict = True, the length of the compressed iterator must be equal, otherwise a ValueError will be thrown. Before Python 3.9, if you were to compress two lists of unequal length, you would get output with a length equal to that of a smaller list.

As shown in the following example, prior to Python 3.10, the zip () function ignored the mismatched'D' in the first list. By contrast, Python 3.10 will raise ValueError. Given the intuitive nature of compressing the same number of items, I like this feature because it wakes you up to re-check your input. Learn more about this issue on PEP 618.

Before Python 3.10:

> list (zip (['Apple',' Ball', 'Cat']) [(' A', 'Apple'), (' Bamboo, 'Ball'), (' 'Cat',' Cat')]

In Python 3.10:

> list (zip (['Apple',' Ball', 'argument,' D'], ['Agar,' Ball', 'Cat'], strict=True) Traceback (most recent call last):... ValueError: zip () argument 1 is longer than argument 2

(3) read-only view of the dictionary

The dictionary's three key methods keys (), values (), and items () return collection-like objects that correspond to the dictionary's keys, values, and dynamic views of items, respectively. Any changes you make in both views will also be reflected in the original dictionary.

In Python 3.10, all views returned from the above three methods will have an additional property called mapping, which will return the read-only proxy for the map. The read-only agent wraps the original dictionary referenced by the view. The following example illustrates this:

Let's define a dictionary and store its keys and values in separate variables:

> fruits = {'Mangos': 12,' Figs': 100, 'Guavas': 3,' Kiwis': 70} > keys = fruits.keys () > values = fruits.values () > list (keys) ['Mangos',' Figs', 'Guavas',' Kiwis']

Now, we use the theory statement to remove two elements from the dictionary. If you print the keys and values now, you will see that it returns only the rest of the items. The changes in the original dictionary are now reflected in the view (keys and values in this case).

> del fruits ['Figs'] > del fruits [' Guavas'] > print (list (keys), list (values)) ['Mangos',' Kiwis'] [12,70]

Now, through mapping, you will still be able to retrieve the read-only agent of the original dictionary. Cool! Isn't it?

# returns a read-only proxy of the original dictionary > > values.mapping mappingproxy ({'Mangos': 12,' Figs': 100, 'Guavas': 3,' Kiwis': 70}) > > values.mapping ['Guavas'] 3

(4) eliminate some backward compatibility

Alias support for the abstract base class (ABC) of the collection module will be removed from Python 3.10. Therefore, now is a good time to stop ignoring the corresponding "deprecation warnings" and adapt the code.

Gao Da Python 3.9.0b4 (released on July 3, 2020)

So far, any of these ABC will be imported directly from the collections module in DeprecationWarning.

> from collections import ABC_Name DeprecationWarning: Using or importing the ABCs from 'collections' instead of from' collections.abc' is deprecated since Python 3.3 it will stop working and in 3.9 it will stop working so far, I believe you have a deeper understanding of "what are the functions in Python 3.10", you might as well come to the actual operation! 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