Noteworthy update points in Python 3.9

Python keeps updating.

2020 On August 19, 2009, Python released the latest version of bate 3.9.0rc1, a new version that is ready to be released, let’s take a look in advance~

new function

Added dictionary merging method

Elegant again. Before version 3.8, dictionary merging needed to use zip() or other methods, but now you only need to use | to quickly complete the expectation. It should be noted that when two dictionaries have the same key, the corresponding The value of is the last assignment:

>>> d ={'spam':1,'eggs':2,'cheese':3}>>> e ={'cheese':'cheddar','aardvark':'Ethel'}>>> d | e
{' spam':1,'eggs':2,'cheese':'cheddar','aardvark':'Ethel'}>>> e | d
{' aardvark':'Ethel','spam':1,'eggs':2,'cheese':3}

To update the dictionary directly, use |=

>>> d |= e
>>> d
{' spam':1,'eggs':2,'cheese':'cheddar','aardvark':'Ethel'}

The logic has [the point is similar to the previous issue] (https://mp.weixin.qq.com/s?__biz=MzUzMTEwODk0Ng==&mid=2247492090&idx=1&sn=939f298867f0843f5fb12891aaeda1c2&chksm=fa4524c7cd32add1ec53409f6f94e47dcf95a8b7dd694ac915137d25e0e60291550020ba52ca&token=1997452966&lang=zh_CN&scene=21#wechat_redirect) What we said in the magic method +=, that is, a+=b is equivalent to a =a+b.

Added built-in functions for string manipulation

removeprefix() and removesuffix(): Although the update is large, the advantages of this are:

To cite a few cases:

# Current
if funcname.startswith("context."):
 self.funcname = funcname.replace("context.","")
 self.contextfunc = True
else:
 self.funcname = funcname
 self.contextfunc = False

# Improved
if funcname.startswith("context."):
 self.funcname = funcname.removeprefix("context.")
 self.contextfunc = True
else:
 self.funcname = funcname
 self.contextfunc = False

Another example:

# Current
if name.endswith(('Mixin','Tests')):return name[:-5]
elif name.endswith('Test'):return name[:-4]else:return name

# Improved
return(name.removesuffix('Mixin').removesuffix('Tests').removesuffix('Test'))

The above can be directly understood as that in the positive and reverse string arrangement, part of the known string content is deleted without using character string slicing.

**Type hint **

Now, based on 3.5, the python editor can quickly respond to the designation and understand our intentions.

In the above figure, we define the parameter of the sum_dict function as a dictionary type, and the return value is defined as an int type. The type is also specified in the definition of test.

Time zone object

The zoneinfo module helps to obtain the corresponding information from the IANA time zone database for optimizing the filling of time zone objects. It is simply used as follows:

>>> print(datetime(2020,2,22,12,0).astimezone())2020-02-2212:00:00-05:00>>>print(datetime(2020,2,22,12,0).astimezone()....strftime("%Y-%m-%d %H:%M:%S %Z"))2020-02-2212:00:00 EST
>>> print(datetime(2020,2,22,12,0).astimezone(timezone.utc))2020-02-2217:00:00+00:00

New parser

Python currently mainly uses a grammar based on LL (1), and this grammar can be parsed by the LL (1) parser-the parser parses the code from top to bottom and from left to right, only from the lexical A token can be taken out of the analyzer to parse it correctly.

This should be the biggest modification point, but since I don't know the working principle of the underlying layer, I will post the official overview here. You can check the original text for details.

This PEP proposes replacing the current LL(1)-based parser of CPython with a new PEG-based parser. This new parser would allow the elimination of multiple "hacks" that exist in the current grammar to circumvent the LL(1)-limitation. It would substantially reduce the maintenance costs in some areas related to the compiling pipeline such as the grammar, the parser and the AST generation. The new PEG parser will also lift the LL(1) restriction on the current Python grammar.

The update log also mentions some language feature changes, module deletions, deprecations, and API changes. Those who are interested can check the official update log to see:

https://docs.python.org/3.9/whatsnew/changelog.html#changelog

Python3.9 version is coming, click on the Python3 album, you will get more information, we will see you in the next issue.


Reference

Python 3.9 beta2 version is released. What are these 7 new PEPs?

The official version of Python 3.9 is coming, and I am still on the way to 3.6!

What’s New In Python 3.9

https://docs.python.org/3.9/whatsnew/3.9.html#what-s-new-in-python-3-9

Recommended Posts

Noteworthy update points in Python 3.9
Functions in python
Python knowledge points
Knowledge points of shell execution in python
03. Operators in Python entry
Python3 supplementary knowledge points
Python advanced knowledge points
Join function in Python
12. Network Programming in Python3
print statement in python
Python data analysis-data update
Concurrent requests in Python
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
Write gui in python
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
Concurrent requests in Python (part 2)
Subscripts of tuples in Python
Talking about inheritance in Python
2020--Python grammar common knowledge points
Containerize Python applications in 3 minutes
How does python update packages
What is introspection in python
What is object-oriented in python
Generators and iterators in Python
Update gcc to 6.4.0 in centos
Talking about strings in Python
How does Python list update value
Use of Pandas in Python development
Use nohup command instructions in python
What is list comprehension in python
Is there function overloading in python
Getting started with Numpy in Python
Detailed sorting algorithm (implemented in Python)
How to wrap in python code
What does rc1 mean in python
Python crawler basic knowledge points finishing
Use of numpy in Python development
What does def in python do
How to omit parentheses in Python
Detailed usage of dictionary in Python
Usage of os package in python
Learn about garbage collection in Python
How to write classes in python
How to filter numbers in python
​What are the numbers in Python?
How to read Excel in Python
There are several keywords in Python
Everything in Python is an object
How to view errors in python
What does np do in python
How to write return in python
Talking about the modules in Python
How to understand variables in Python
How to clear variables in python
The usage of tuples in python
How to use SQLite in Python
Description of in parameterization in python mysql