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~
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
.
removeprefix()
and removesuffix():
Although the update is large, the advantages of this are:
len
and str.replace()
functionsTo 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.
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.
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
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