In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what are the new features of Python3.10, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Further extension of type annotations
Type hints and comments in Python seem to be a continuing trend and have been further extended in 3.10, obviously for better readability and to know the types of variables and function return values without looking at the code.
Delay the execution of type comments
The operation of type comments is generally considered to be performed at function definition, which means that type comments are checked line by line in a top-down manner.
Logical as it may seem, there are two problems with doing so:
1. Type hints (forward references) that reference an undefined type will not work and must be expressed as a string. That is: if int is a custom type, we need to write "int" instead of int.
2. This slows down the module import because the type prompt is performed.
Therefore, instead of delaying type comments, type comments will be stored as strings in _ _ annotations__. If you need these type comments, you can parse them at run time through typing.get_type_hints (), or you can parse them immediately through inspect.signature (). This advantage is that you can perform module import first, allowing forward references, thus reducing initialization time.
New type annotation federation operator
3.10 use "|" as logic or operator. When annotating data types, we can use | as or. For example, we have a variable that is expected to be int or float, which can be written as int | float, as follows:
Def f (x: int | float)-> float: return x * 3.142f (1) # passf (1.5) # passf ('str') # linter will show annotation error
You can also use the keyword Union provided by the typing module, 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 these types to variables, because Python assumes that our string literal type comment is just a string.
Using a type annotation variable where a type annotation is commonly used will return an error. For example:
MyType = "ClassName" # ClassName is our type annotationdef foo ()-> MyType:...
Here, we try to use its MyType as an alias for the type, but MyType it will be read as a string value, not a type alias. This is valid as long as ClassName is defined at the end of the code. In the current case, this will cause an annotation error.
To solve this problem, a method has been added to explicitly identify MyType as a type alias:
From typing_extensions import TypeAliasMyType: TypeAlias = "ClassName" def foo ()-> MyType:... ORMyType: TypeAlias = ClassName # if we have defined ClassName alreadydef foo ()-> MyType:.
Here's why types are important, although it's certainly not a big change, but it's cool to see Python developers redouble their efforts to enhance types. The advantage of Python lies in its ease of use and lack of a steep learning curve. One of the reasons is that there is no need to explicitly define types in our code.
Enhanced type annotations may seem counterintuitive, but providing developers with the option to define types can greatly improve the readability and maintainability of the code base. For example, you can see the following instructions from the source code of the Python transformers library:
Even without context, we can read this code and immediately grasp what data we should expect to be fed into these functions, classes, and methods-and exactly which datatypes we should be expecting to return.
In complex code bases (and even simple ones), type annotation can massively improve readability. Simultaneously, not everyone will want (or need) to use them-so an optional, exception-free functionality strikes a perfect balance.
It means that even if there is no context, we can read this code and immediately know which data is expected to be entered into these functions, classes, and methods, and exactly which data types are expected to be returned.
But in complex code bases (or even simple ones), type annotations can greatly improve readability. At the same time, not everyone wants (or needs) to use them, so this is optional. This non-abnormal function can achieve a perfect balance.
These improvements demonstrate Python's commitment to type annotation, based on which our favorite libraries and our own code can greatly prompt readability, which can have a long-term positive impact on the Python ecosystem.
2. The new function and the change of function parameter
In addition to the extension of the type hint function, the core Python functionality has been updated as follows.
The function zip () adds the strict parameter
The function zip () adds the strict parameter. If strict = True is set, and the length of the transmitted parameter is not equal, an exception will be thrown, as shown in the following figure:
Instead of blindly truncating mismatched data, the new strict parameter enables us to control its behavior, which will keep many developers out of trouble.
Add bit counter int.bit_count () for integers
This new method enables us to calculate the number of ones in the binary representation of integers, which is very practical and efficient in some scenarios.
The result in the above figure is the number of integers with a binary bit of 1:
0 = 00000000
1 = 00000001
2 = 00000010
3 = 00000011
10 = 00001010
11 = 00001011
12 = 00001100
100 = 01100100
101 = 01100101
10 2 = 01100110
Add an attribute to the view of the dictionary
The three methods of the dictionary type: dict.items (), dict.keys (), and dict.values () return three views of the dictionary, respectively. Now each view adds an attribute called mapping. The specific usage is as follows:
The new property mapping is of type types.MappingProxyType and is an attribute around the original dictionary. Accessing the mapping property on any view returns the original dictionary.
That's all for now, and although we are only a few months away from the 3.10 development schedule, there have been a lot of interesting changes, and the development of Python is still going on, and it seems that more interesting features will be added to the language.
These are all the contents of the article "what are the New Features in Python3.10". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.