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 Python3.8

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what are the new features of Python3.8". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the new features of Python3.8"?

>? This article takes about 2 minutes to read. Then you will learn:

How to use a new assignment method in expression: =

How to restrict the parameter type / when defining a method

Better debugging skills f "{expr=}"

Other python38 features that you need to adapt to

0x01 walrus assignment method

This will be the biggest change in the history of python, and its writing is very similar to that of go. So that the assignment of a variable can occur inside the expression. As for why it is called walrus assignment..

Let's move on to the following two examples

Conventional method

A = [0Jing 1Jing 2JI 3JI 4] n = len (a) if n

< 10:print(f"数据长度为{n}, 且小于10") 使用海象赋值法 a = [0,1,2,3,4]if (n := len(a)) < 10: print(f"数据长度为{n}, 且小于10") 第二个方法相比第一次减少了 n = len(a) 的赋值过程,转而写在了if表达式当中,并使n可以继续在下文中使用。 再例如列表推导式,我们需要判断迭代每项i的计算结果ii是否符合条件,并将结果生成列表。 [ii for i in range(1,101) if (ii := i**2 ) >

200]

Try to limit the use of walrus operators to clear situations to reduce complexity and improve readability.

0x02 is limited to position parameters only

(PEP 570) added a function parameter syntax / to indicate that some function parameters must be in the form of position-only rather than keyword arguments.

For example, the demo method, a before / can only use positional parameters, and c after * can only use keyword parameters.

Def demo (a, /, b, *, c): print (a, b, c) demo (1,2,3) # correct demo (aversion 1,2,3) # error, a unavailable keyword parameter demo (1,2,3) # error, c unavailable positional parameter 0x03 f'{expr=}'to record expressions and debugging documents

(bro-36817) add the = specifier for f-string, which outputs additional reference values of variables, such as:

Import momenttoday = moment.now () print (f "{today=}, have a great time ~") # output today=, have a great time ~ 0x04 some common language features updates and performance changes (✨ briefing)

Here, we have selected some more commonly used features and updates to briefly list them.

❌ yield expressions will not be allowed in deductions and generators

✅ continue statements will be allowed in finally expressions

✅ dict can now be reversed in insertion order through the reversed method

✅ asyncio's task can now be named

The default protocol used by the ✅ pickle module is now Protocol 4, which is short and lean

Copy-related methods on ✅ shutil modules are 26% to 50% more efficient

✅ typing module adds Literal and Final, so type checking has a long way to go.

✅ pathlib and os.path will return False for some abnormal character paths instead of reporting an error

Dist,hypot,prod,isqrt4 new functions have been added to the ✅ math module. Check the details

In addition to the above, visit https://docs.python.org/zh-cn/3.8/whatsnew/3.8.html for more new features

Thank you for your reading, the above is the content of "what are the new features of Python3.8". After the study of this article, I believe you have a deeper understanding of what new features of Python3.8 are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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