In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the new features of Python 3.8". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the new features of Python 3.8"?
1. New module: reading metadata calendar
The metadata of third-party packages can be read using the new importlib.metadata module. You can get the version number in the script package.
two。 Dictionary reversal: sorting
Now, the dictionary can iterate in reverse insertion sorting with resersed ().
3. Continue-finally
Due to the implementation of Python, it was illegal to add a continue statement to the Finally clause before. Now this problem no longer exists.
For i inrange (2): try: print (I) finally: print ('A sentence.') Continue print ('This never shows.') # Python > SyntaxError:' continue'not supported inside 'finally' clause # Python 3.8 > > 0A sentence. 1 A sentence.
4. Indicate parameter type: make the code more powerful
The Python function accepts only two arguments.
Position parameters: pass parameters according to the position of the parameters defined by the function.
Keyword parameters: use keywords to specify parameters.
In the following example, the values of parameter an and parameter b can be determined by location parameters or keyword parameters, which is very flexible.
Def my_func (a, baggage 1): return a+bmy_func (5, 2) # both positional arguments my_func (axiom, 5, 2) # both keyword arguments
The new version of Python distinguishes between syntax / and * and provides a way to specify to accept positional or keyword parameters. Note: syntax * does not appear only in Python 3.8.
In the following example, the first two parameters an and b accept only position parameters, the middle c and d accept position parameters or keyword parameters, and the last e and f accept only keyword parameters.
Defmy_func (a, b, /, c, d, *, e, f): return a+b+c+d+e+f my_func (1 returns 2) # returns 21 my_unc (1 returns 2) # returns 21 my_unc (1) # returns 21
Why sacrifice flexibility? If the parameter name is arbitrary and useless, the keyword parameter should be excluded. If you expect the parameter name in the function to change, specifying the parameter type can prevent the code from crashing and make the code more powerful.
5. Assignment expressions: improve code readability
An assignment expression, also known as the walrus operator, is a new operator in the form of: =, which assigns a part of a larger expression to a variable. This is undoubtedly the most discussed new feature in Python 3.8.
Let's take an example: the assignment expression b: = axiomatic 2 assigns the square of a, that is, 36, to b, and then checks to see if the value of b is greater than 0.
A = 6 # The following statement # assigns the value a * * 2 to variable b, # and then check if b > 0 is true if (b: = a * * 2) > 0: print (f'The square of {a} is {b}.) # The square of 6 is 36.
Sometimes assignment expressions make the code more concise and readable. However, be careful not to abuse the assignment expression, because in some cases it can make the code more difficult to understand.
# DON'T DO THIS! A = 5 d = [b: = aqui1, a: = bmuri 1, a: = aqui2]
Assignment expressions are also available in other (old) programming languages, and programmers who want to switch to Python will (more) take advantage of this new feature. Master assignment expressions before they flood.
F string 2. 0: a powerful tool for debugging (debug)
Python f string changes the rules of the game. The F string is the syntax for formatting strings, concise and easy to read. With the syntax f'{expr}', you can insert the expression into a string by adding f before the quotation marks and enclosing the expression in curly braces.
After the Python update, the equals sign "=" can be used as a format qualifier in the f string f'{expr=} 'syntax. The output string is the variable name = variable value, as follows:
# DON'T DO THIS! A = 5 d = [b: = aqui1, a: = bmuri 1, a: = aqui2]
For the purpose of documentation or Debug, it is often necessary to specify the value of the variable. The F string makes debugging (debug) easy.
Of course, before you can devote yourself to Python 3.8 programming, you need to master the basic functions of Python (essential).
At this point, I believe you have a deeper understanding of "what are the new features of Python 3.8"? 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.
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.