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 new features of Python 3.10

2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what are the new features of Python 3.10". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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 = 108

# Let's first get the binary representation of num

> bin (num)

'0b1101100'

> > num.bit_count ()

four

# Negative integer

> num =-108

> bin (num)

'- 0b1101100'

> > num.bit_count ()

four

# Under the hood

> bin (num) .count ('1') 2. 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'])

[('Agar,' Apple'), ('Ball',' Cat')]

In Python 3.10:

> list (zip (['Apple',' Ball', 'Cat'], strict=True))

Traceback (most recent call last):... ValueError: zip () argument 1 is longer than argument 2 3. A 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. Gundam Python 3.9.0b4 (released July 3, 2020) until now, 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 and in 3.9 it will stop working, what are the new features of Python 3.10? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report