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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the new features of Python3.9". In daily operation, I believe many people have doubts about the new features of Python3.9. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the new features of Python3.9?" Next, please follow the editor to study!
Python has released a beta version with the version number 3.9.0b3, followed by an official version of Python 3.9. This version contains some exciting new features that are expected to be widely used after the official release.
This paper mainly introduces the following aspects:
New dictionary merge operation type hint string added two new methods new Python parser-awesome!
Next, let's take a look at these features and their usage.
1. Dictionary merging
This is my favorite new feature, and its usage is very elegant. If you want to merge the two dictionaries an and b, we can use the merge operation with the operator |.
A = {1:'a', 2: 'baked, 3:' c'}
B = {4: 'dwells, 5:' e'}
C = a | b
Print (c)
The results are as follows:
You can also use the merge update operation | =, which updates the data of the original dictionary.
A = {1:'a', 2: 'baked, 3:' c'}
B = {4: 'dwells, 5:' e'}
A | = b
Print (a)
The results are as follows:
When performing a merge operation, if the dictionary contains the same key, the result of the operation takes the key-value pair of the second dictionary.
A = {1: 'ajar, 2:' baked, 3: 'crested, 6:' in both'}
B = {4: 'daddy, 5:' eBay, 6: 'but different'}
Print (a | b)
The results are as follows:
two。 Dictionary update of iterable objects
| another great feature of the = operator is to update the dictionary with the key-value pair of an iterable object, such as a list or generator.
A = {'averse:' one', 'baked:' two'}
B = (I, iTunes 2) for i in range (3))
A | = b
Print (a)
The results are as follows:
If you use the standard merge operator | replace the above | = operator, because the | operator only allows dictionary types to merge, it will result in TypeError.
3. Type hint
Python is a dynamically typed programming language, which means we don't need to specify data types in our code.
While this can be done, it can sometimes be confusing, and flexibility becomes a disadvantage.
Starting with version 3.5, we can specify the type, but it's still very troublesome. This time this has been improved, let's take a look at the following example.
For some mysterious reason, we want to add the same numbers together through the add_int function shown in the figure above. But the editor doesn't quite understand our intentions, and there is no warning because two strings can also be concatenated with +.
Now that we can specify the parameter type of the function as int, the editor can immediately identify the above problem.
Through the type hint, we can also see very specific types.
Type hints can be used anywhere, and they look much more refreshing with the new syntax support.
4. New string function
Although the string function is not as "great" as other features, it is worth mentioning here because it is very practical. Two string functions to remove prefixes and suffixes have been added in the new version.
"Hello world" .removeprefix ("He")
The results are as follows:
Hello world ".removesuffix (" ld ")
The results are as follows:
5. New parser
Developers are not easily aware of the changes brought about by the new syntax parser, but it has the potential to be an important shift in the evolution of Python.
Python currently mainly uses a syntax based on LL (1), which can be parsed through the LL (1) parser-- which parses the code from top to bottom and from left to right, and only needs to pull a token from the lexical analyzer to parse correctly.
I don't know exactly how it works, but I can give you some problems with LL (1):
Python contains non-LL (1) syntax, which is why the current grammar adopts some curvilinear methods to save the nation, which brings a lot of unnecessary complexity. LL (1) places a lot of restrictions on Python syntax. A related topic mentions that the following code cannot be parsed with the current parser (causing SyntaxError). With (open ("a_really_long_foo") as foo
Open ("a_really_long_bar") as bar):
Pass
LL (1) cannot handle left recursion. A specific recursive syntax means that an infinite loop may occur when parsing a tree. Guido van Rossum, the founder of Python, gives an explanation in this article [1].
These and other factors that I do not understand affect the development of Python, and they limit the evolution of the language.
The new parser, based on PEG, will give Python developers more flexibility, and we will be able to feel this starting with Python 3.10 [2].
At this point, the study on "what are the new features of Python3.9" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.