Come here, come here, the official version of Python 3.9 is released~~~

Abstract: On October 5, 2020, when people across the country celebrated the National Day and Mid-Autumn Festival, Python 3.9 was quietly officially released.

On October 5, 2020, when people across the country celebrated the National Day and Mid-Autumn Festival, Python 3.9 was officially released quietly. Let's take a look at the fun new features of this version and what impact it may have on the current products of our department.

Because tools such as jupyter notebook/lab have not been adapted to python 3.9, we cannot use them yet, so this article uses python interactive lines to demonstrate.

The official Python 3.9 document, What's New in Python 3.9 , is well organized, and we will explain it in this order, followed by release highlights, new features, new modules, improve modules, optimizations, deprecated, removed. Everyone Note that this text organization sequence is actually applicable when our product is released. Let me first talk about the attractive highlights of this version, then introduce the new content of the new version, and finally introduce deprecated/removed, reminding everyone what to pay attention to when upgrading, and the organization is very clear.

installation

As of October 9, 2020, there is no channel on anaconda that supports the direct installation of python 3.9, so if you want to try it, there are two ways:

1. Download the installation package on python.org

2. Go to the conda-forge channel of anaconda to download the installation file

We use the second method, see References for the installation file download link.

	$ conda create -n py39 -c conda-forge -y
	$ conda activate py39
	$ conda install python-3.9.0-h60c2a47_1_cpython.tar.bz2
	 
	$ which python
	/d/Anaconda3/envs/py39/python
	$ python -V
	Python 3.9.0

Release Highlights

Python 3.9 includes:

  • 3 new grammatical features
  • 1 new built-in feature
  • 2 new standard library features
  • 6-point interpreter improvement
  • 2 new library modules

If you go through all of the above, it may take 1-2 hours. Let's pick some content related to our department's product development. Specifically, if you are interested in other content, you can read it yourself.

New Features

Dictionary Merge & Update Operators

The dict class provides merge (|) and update (|=) operators.

	# py38
	>>> x = {"key1": "value1 from x", "key2": "value2 from x"}
	>>> y = {"key2": "value2 from y", "key3": "value3 from y"}
	>>> {**x, **y}
	{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}
	>>> x.update(y)
	>>> x
	{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}
	 
	# py39
	>>> x = {"key1": "value1 from x", "key2": "value2 from x"}
	>>> y = {"key2": "value2 from y", "key3": "value3 from y"}
	>>> x | y
	{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}
	>>> y | x
	{'key2': 'value2 from x', 'key3': 'value3 from y', 'key1': 'value1 from x'}

This is more convenient for dict operations.

New string methods to remove prefixes and suffixes

	>>> "NavyXie".removeprefix("Navy")
	'Xie'
	>>> "NavyXie".removesuffix("Xie")
	'Navy'

This is more convenient when deleting unnecessary prefix or suffix from string.

Type hinting generics in standard collections

In the type annotation, you can use the built-in collection types, such as list and dict, instead of importing the corresponding uppercase types, such as typing.List or typing.Dict.

	def greet_all(names: list[str]) -> None:
	    for name in names:
	        print("Hello", name)

Annotation is a feature introduced by python 3.0. What is it for? Unlike strongly typed languages ​​such as Java / C / C++ / Swift, Python and JavaScript are both weakly typed languages. Here, type annotations do not force the type of parameters to be passed during parsing or runtime, but only help developers to read and write code. maintain.

In addition, if we use the library introduced by python 3.7, dataclasses, we will find that type annotation is mandatory when defining a data class, such as:

	>>> from dataclasses import dataclass
	>>> @dataclass
	... def TestClass:
	...    name: str
	...
	>>> TestClass.__annotations__
	{'name': <class 'str'>}

It will be more useful at this time, we can write:

	names: list[str]

Instead of as before:

	names: List[str]

New parser

Python 3.9 started to use a new parser, based on PEG, instead of LL(1). The performance of the two is not much different, but PEG is more flexible. From here we can infer that starting from Python 3.10, more new language features will be introduced.

zoneinfo

This new module will be more convenient when we manipulate the time zone. When we dealt with timezone before, we needed to pass the pytz package, such as:

	# py38
	import pytz
	from datetime import datetime
	 
	tz = pytz.timezone("America/Los_Angeles")
	start_time = datetime.now(tz)

Now you can use the zoneinfo module in the standard library, such as:

	from zoneinfo import ZoneInfo
	 
	tz = ZoneInfo("America/Los_Angeles")

Other changes

In python 3.8, the Vectorcall protocol was temporarily introduced. In 3.9, the built-in types, including range, tuple, set, frozenset, list, and dict, are optimized using the vectorcall protocol. But what is interesting is that from the performance optimization report, we can see that the performance from 3.8 to 3.9 has not improved, and even dropped slightly.

	Python version                       3.4     3.5     3.6     3.7     3.8    3.9
	--------------                       ---     ---     ---     ---     ---    ---
	 
	Variable and attribute read access:
	    read_local                       7.1     7.1     5.4     5.1     3.9    4.0
	    read_nonlocal                    7.1     8.1     5.8     5.4     4.4    4.8
	    read_global                     15.5    19.0    14.3    13.6     7.6    7.7
	    read_builtin                    21.1    21.6    18.5    19.0     7.5    7.7
	    read_classvar_from_class        25.6    26.5    20.7    19.5    18.4   18.6
	    read_classvar_from_instance     22.8    23.5    18.8    17.1    16.4   20.1
	    read_instancevar                32.4    33.1    28.0    26.3    25.4   27.7
	    read_instancevar_slots          27.8    31.3    20.8    20.8    20.2   24.5
	    read_namedtuple                 73.8    57.5    45.0    46.8    18.4   23.2
	    read_boundmethod                37.6    37.9    29.6    26.9    27.7   45.9
	 
	Variable and attribute write access:
	    write_local                      8.7     9.3     5.5     5.3     4.3    4.2
	    write_nonlocal                  10.5    11.1     5.6     5.5     4.7    4.9
	    write_global                    19.7    21.2    18.0    18.0    15.8   17.2
	    write_classvar                  92.9    96.0   104.6   102.1    39.2   43.2
	    write_instancevar               44.6    45.8    40.0    38.9    35.5   40.7
	    write_instancevar_slots         35.6    36.1    27.3    26.6    25.7   27.7
	 
	Data structure read access:
	    read_list                       24.2    24.5    20.8    20.8    19.0   21.1
	    read_deque                      24.7    25.5    20.2    20.6    19.8   21.6
	    read_dict                       24.3    25.7    22.3    23.0    21.0   22.5
	    read_strdict                    22.6    24.3    19.5    21.2    18.9   21.6
	 
	Data structure write access:
	    write_list                      27.1    28.5    22.5    21.6    20.0   21.6
	    write_deque                     28.7    30.1    22.7    21.8    23.5   23.2
	    write_dict                      31.4    33.3    29.3    29.2    24.7   27.8
	    write_strdict                   28.4    29.9    27.5    25.2    23.1   29.8
	 
	Stack (or queue) operations:
	    list_append_pop                 93.4   112.7    75.4    74.2    50.8   53.9
	    deque_append_pop                43.5    57.0    49.4    49.2    42.5   45.5
	    deque_append_popleft            43.7    57.3    49.7    49.7    42.8   45.5
	 
	Timing loop:
	    loop_overhead                    0.5     0.6     0.4     0.3     0.3    0.3

Note : The above result is the running result of the official python benchmark, Tools/scripts/var_access_benchmark.py, in nanoseconds, the hardware is Intel® Core™ i7-4960HQ processor, and the OS is macOS 64-bit.

注意 Deprecated / Removed

I extracted some points that may be more relevant to our department’s products:

(1) Python 3.9 is the last version that provides backward compatibility with Python 2, so in the next version, Python 3.10 will no longer be compatible with Python 2.

(2) The isAlive() method of the threading.Thread class is deleted and replaced with is_alive().

(3) base64.encodestring() and base64.decodestring() have been deleted and replaced with base64.encodebytes() and base64.decodebytes().

(4) The encoding parameter of json.loads() is deleted, and encoding must be UTF-8, UTF-16 or UTF-32.

Review some features of Python 3.8

Finally, let's review a few new features of python 3.8. If you haven't tried it in your work, then try it now.

  • Walrus operator: =
	if (n := len(a)) > 10:
	    print(f"List is too long ({n} elements, expected <= 10)")
  • Positional-only parameters
	def f(a, b, /, c, d, *, e, f):
	    print(a, b, c, d, e, f)
  • f-string support =
	>>> user = 'eric_idle'
	>>> member_since = date(1975, 7, 31)
	>>> f'{user=} {member_since=}'
	"user='eric_idle' member_since=datetime.date(1975, 7, 31)"
	 
	>>> delta = date.today() - member_since
	>>> f'{user=!s}  {delta.days=:,d}'
	'user=eric_idle  delta.days=16,075'
	 
	>>> print(f'{theta=}  {cos(radians(theta))=:.3f}')
	theta=30  cos(radians(theta))=0.866

References

 

Click to follow and learn about Huawei Cloud's fresh technology for the first time~

Guess you like

Origin blog.csdn.net/devcloud/article/details/109022837