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 characteristics of Python 3.9?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what are the characteristics of Python 3.9". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the characteristics of Python 3.9"?

Python 3.9 was officially released on Oct. 5, 2020, when people all over the country celebrated National Day and Mid-Autumn Festival. Let's take a look at what interesting new features this version has and what impact it may have on our department's current products.

Because tools such as jupyter notebook/lab haven't been adapted to python 3.9 yet, we can't use them yet, so this article uses the interactive line of python to demonstrate.

Python 3.9 official document, What's New in Python 3.9, its text is well organized, and we will explain it in this order: release highlights, new features, new modules, improve modules, optimizations, deprecated, removed. Please note that this text organization order is also applicable when our product is released. First talk about the attractive highlights of this version, then introduce the new content of the new version, and finally introduce deprecated / removed to remind you of what you need to pay attention to when upgrading.

Installation

As of October 9, 2020, there is no channel support for direct installation of python 3.9 on anaconda, so there are two ways to try it:

1. Download the installation package on http://python.org

two。 Download the installation file at anaconda's conda-forge channel

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

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

Python 3.9 includes:

Three new grammatical features

1 new built-in feature

2 new standard library features

6-point interpreter upgrade

2 new library modules

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

New FeaturesDictionary Merge & Update Operators

The dict class provides the 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 when operating with dict.

New string methods to remove prefixes and suffixes > "NavyXie" .removeprefix ("Navy") 'Xie' > "NavyXie" .removesuffix ("Xie")' Navy'

This is more convenient when string removes unwanted prefix or suffix.

Type hinting generics in standard collections

In type annotation, you can use built-in collection types, such as list and dict, instead of importing 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 used for? Unlike strongly typed languages such as Java / C / C++ / Swift, Python and JavaScript are weakly typed. Here, type annotation does not require the type of parameter to be passed at parsing or run time, but only helps developers read and maintain the code.

In addition, if we use the library, dataclasses, introduced by python 3.7, 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':}

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

Names: list [str]

Instead of having to do what it did before:

Names: List [str] New parser

Python 3.9 began using 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 this we can infer that more new language features will be introduced starting with Python 3.10.

Zoneinfo

This new module will be more convenient when we operate the time zone. Previously, when we were dealing with timezone, we needed to use the pytz package, such as:

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

You can now 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, and in 3.9, built-in types, including range, tuple, set, frozenset, list, dict, are optimized using the vectorcall protocol. But interestingly, from the performance optimization report, we can see that there is no improvement or even a slight decline in performance from 3.8 to 3.9.

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 results are the running results of python's official benchmark, Tools/scripts/var_access_benchmark.py, in nanoseconds, the hardware is Intel ®Core ™i7-4960HQ processor, and OS is macOS 64-bit.

Note Deprecated / Removed

I have extracted some points that may be highly relevant to the products of our department:

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

(2) the isAlive () method of the threading.Thread class is removed and replaced by is_alive ().

(3) base64.encodestring () and base64.decodestring () are deleted and replaced by base64.encodebytes () and base64.decodebytes ().

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

Review several features of Python 3.8s

Finally, let's review a few new features of python 3.8. if you haven't tried it at work, try it right away.

Walrus operator: =

If (n: = len (a)) > 10: print (f "List is too long ({n} elements, expected)

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

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report