Home Backend Development Python Tutorial Learn about the new features of Python 3.8 in one article

Learn about the new features of Python 3.8 in one article

Aug 19, 2019 pm 05:58 PM
new function

Learn about the new features of Python 3.8 in one article

Python 3.8 is the latest version of the Python language, which is suitable for various tasks such as scripting, automation, and machine learning and web development. Now that Python 3.8 has entered the official beta stage, this version brings many syntax changes, memory sharing, more efficient serialization and deserialization, improved dictionaries, and more new features.

Python 3.8 also introduces many performance improvements. Overall, we are about to have a faster, more precise, more consistent, and more modern Python. Below are the new features and most important changes in Python 3.8.

1. Assignment expressions

The most obvious change in Python 3.8 is the assignment expression, that is, the := operator. Assignment expressions can assign a value to a variable even if the variable does not exist. It can be used in expressions without appearing as a separate statement.

Learn about the new features of Python 3.8 in one article

In the above example, if the variable line does not exist, it will be created, and then the return value of file.readline() will be assigned to it. Then check if line is "end". If not, read the next line, save it in line, and continue testing.

Assignment expressions follow Python’s tradition of simplicity, just like list comprehensions. Its purpose is to avoid some boring boilerplate code in a specific Python programming model. For example, the above code requires two more lines of code to be written in a normal way.

Related recommendations: "Python Video Tutorial"

2. Only parameters specified by position

Only Positionally specified arguments are a new syntax in function definitions that allow programmers to force a parameter to be specified positionally only. This resolves the ambiguity in Python function definitions about which parameters are positional parameters and which parameters are keyword parameters.

Parameters specified only by position can be used in the following situations: a function accepts any keyword parameters, but can also accept one or more unknown parameters. This is often the case with Python's built-in functions, so allowing programmers to do this enhances the consistency of the Python language.

The example given in the Python documentation is as follows:

Learn about the new features of Python 3.8 in one article

#The symbol / separates positional parameters and keyword parameters. In this example, all parameters are unknown parameters. In previous versions of Python, z would be considered a keyword argument. But using the above function definition, pow(2, 10) and pow(2, 10, 5) are both correct calling methods, but pow(2, 10, z=5) is incorrect.

3. Support f-string debugging

f-string format makes it easier to calculate output text and values ​​or variables within the same expression, and higher efficiency.

Learn about the new features of Python 3.8 in one article

Output 4.

If = is not added at the end of the f string expression, the value of the f expression itself can be output, followed by the calculated value

Learn about the new features of Python 3.8 in one article

The output is x 1 =4.

4. Multi-process shared memory

In Python 3.8, the multiprocessing module provides the SharedMemory class, which can create shared memory areas between different Python processes. .

In older versions of Python, data shared between processes could only be written to a file, sent through a network socket, or serialized using Python's pickle module. Shared memory provides a faster way to transfer data between processes, making multi-processor and multi-core programming in Python more efficient.

Shared memory segments can be allocated as a simple byte area, or as an unmodifiable list-like object, which can store numeric types, strings, byte objects, None objects, etc. A small collection of Python objects.

5. Improvements in the Typing module

Python is a dynamically typed language, but type hints can be added through the typing module to allow third-party tools to verify Python code. Python 3.8 adds some new elements to typing so that it can support more robust checks:

The final modifier and the Final type annotation indicate that the modified or annotated object should not be overridden at any time , inheritance, and cannot be reassigned.

Literal type limits the expression to a specific value or list of values ​​(not necessarily values ​​of the same type).

TypedDict can be used to create dictionaries whose values ​​for specific keys are restricted to one or more types. Note that these restrictions are only used to determine the legality of values ​​at compile time and cannot be restricted at runtime.

6. New version of pickle protocol

Python's pickle module provides a method to serialize and deserialize Python data structures or instances, and the dictionary can be saved intact for later reading. Different versions of Python support different pickle protocols, and the latest version supports wider, more powerful, and more efficient serialization.

The fifth version of the pickle protocol introduced in Python 3.8 can use a new method to pickle objects, which can support Python's buffer protocol, such as bytes, memoryviews or Numpy array, etc. The new pickle avoids many memory copy operations when pickling these objects.

NumPy, Apache Arrow and other external libraries support the new pickle protocol in their respective Python bindings. The new pickle is also available as a plugin for Python 3.6 and 3.7, which can be installed from PyPI.

7. Reversible Dictionary

The dictionary has been rewritten in Python 3.6, using a new implementation contributed by the PyPy project. In addition to being faster and more compact, dictionaries now inherit the order of their elements - elements are arranged in the order they were added, just like a list. Python 3.8 also allows reversed() on dictionaries.

8. Performance improvements

The speed of many built-in methods and functions has been increased by 20%~50%, because many functions previously required unnecessary parameter conversion .

A new opcode cache improves the speed of specific instructions in the interpreter. However, the only speed improvement currently achieved is the LOAD_GLOBAL opcode, which is 40% faster. Similar optimizations will be made in future versions.

File copy operations such as shutil.copyfile() and shutil.copytree() now use platform-specific calls and other optimizations to improve operation speed.

Newly created lists are now on average 12% smaller than before, thanks to optimizations performed by the list constructor if the list length is known ahead of time.

Write operations to class variables of new types of classes (such as class A(object)) become faster in Python 3.8. operator.itemgetter() and collections.namedtuple() have also been speed optimized.

9.Python C API and CPython implementation

Recent versions of Python have been refactored in terms of the C API used in CPython, the reference implementation of Python written in C. A lot of effort was put into it. So far, this work is still being added, and the existing results include:

Python Initialization Configuration (Python Initialization Configuration) has a new C API that allows for tighter control over Python initialization routines and More detailed feedback. This makes it easier to embed the Python runtime into other applications, and to programmatically pass startup parameters to Python programs. The new API also ensures that all Python configuration controls have a single, consistent location, so future changes (such as Python's new UTF-8 mode) are easier.

CPython's other new C API - the "vectorcall" calling protocol - enables faster calls to Python's internal methods without creating temporary objects. The API is still unstable, but has improved significantly. The API is planned to mature in Python 3.9.

The Python runtime audit hook provides two APIs for the Python runtime, which can be used to hook events to ensure that external tools such as test frameworks, logs, and audit systems can monitor them.

10. How to download Python 3.8

Download the Python 3.8 beta version from the Python Software Foundation: https://www.python.org/downloads/release/python -380b1/

The above is the detailed content of Learn about the new features of Python 3.8 in one article. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

iPadOS18 new features iPadOS18 new features Jun 12, 2024 pm 10:41 PM

Early this morning, Apple officially released iPadOS18. This system not only has the classic functions of iOS18, but also adds some unique functions, such as supporting mathematical note calculators, etc., which further improves the experience of iPad users. Friends who are interested Come and take a look. This time iPadOS18 not only perfectly inherits the core functions of iOS18, such as the personalized control center design, which allows users to freely adjust the order and layout of control items according to personal preferences, and the highly anticipated game mode, providing gamers with smoother and more The immersive gaming experience also incorporates a number of unique features specifically targeting the iPad’s large screen advantages and the creative uses of Apple Pencil, further expanding the iPad’s productivity.

WeChat launches new feature: 'Quiet Mode” WeChat launches new feature: 'Quiet Mode” Mar 04, 2024 pm 01:43 PM

It is understood that WeChat has launched a new feature: "Quiet Mode". After turning it on, WeChat will no longer make sounds in all scenarios such as message notifications, audio and video calls, and video account content playback. Update to the latest version of WeChat on iOS and turn it on through "WeChat" - "Me" - "Settings" - "Care Mode" - "Quiet Mode". After that, WeChat will no longer make sounds in all scenarios such as message notifications, audio and video calls, and video account content playback. WeChat said the feature was developed to meet the needs of those who cannot hear. For most people, sound may be very common and accessible, but they often ignore that the deaf community does not have this perception ability, although they also use mobile phones and WeChat like everyone else. according to

Microsoft Edge browser's new feature 'Super Drag” is now available! Microsoft Edge browser's new feature 'Super Drag” is now available! Mar 04, 2024 pm 06:31 PM

The editor recently learned that the new feature of Microsoft Edge browser "Super Drag" has been launched, unlocking the fourth way to open links in new tabs, making it easier for users to open links faster. Currently, in the Microsoft Edge browser, if users want to open a link or image in a new tab, there are three ways: 1. Right-click the link or image, and then select the corresponding operation option. 2. Drag the link or image to the tab bar. 3. Use the mouse wheel to click on the link or image. "Super Drag" brings a fourth kind of interaction, where users click on a link, part of text, or image and then drag it sideways, up, or down a little to open it in a new tab. After the user drags the text, the default search engine of the Edge browser will be called by default and a new tab will be opened.

iOS18 new features revealed iOS18 new features revealed Mar 06, 2024 pm 02:25 PM

As Apple is about to release iOS 18, its major annual software update, this will undoubtedly become one of the hot and highly anticipated releases in 2024. According to rumors, this update will be regarded as one of the largest software upgrades in the history of the iPhone. Here is a brief summary of the relevant function upgrades. New iOS 18 feature revelations 1. It is reported that Apple may integrate the visionOS style into iOS 18. , giving people a refreshing operating experience. 2. According to rumors, the company is actively testing AI models and is expected to make major improvements to Siri on the iOS18 version. 3. In addition, Apple has recently launched a pilot program and launched a new tool called "Ask" to AppleCare, aiming to improve the efficiency of customer inquiries. 4

Windows 11 details new features Windows 11 details new features Dec 23, 2023 pm 03:37 PM

The new win11 operating system has brought many new functions and features to users, which are very novel, easy to use and powerful. Today we will bring you a detailed introduction to the new features of win11. If you are not sure, come here for details. Let’s find out. What are the new features of win11: 1. The new design of Win11 makes the UI look simple and beautiful, and more modern. 2. Microsoft said that the start menu and application icons in the center of the taskbar can allow users to find what they need more quickly. 3. The update package of Win11 is 40% less than that of Win10 on average, which means it saves more power and runs faster. 4. The speed of the built-in Edge browser has also been improved. On Win11, multiple task arrangement methods have been added.

An in-depth analysis of the new features of PHP8 and its improvement in development efficiency An in-depth analysis of the new features of PHP8 and its improvement in development efficiency Jan 13, 2024 am 11:33 AM

In-depth analysis of the new features of PHP8 to improve development efficiency! PHP8 is the latest version of the PHP programming language, officially released on November 26, 2020. This version brings many exciting new features and improvements that can greatly improve development efficiency. In this article, we’ll take a deep dive into some of PHP8’s new features and provide concrete code examples. 1. JIT compiler PHP8 introduces the JIT (Just-In-Time) compiler, which can compile some popular code blocks in real time, thereby improving

Introduction to new features in PHP 8.3: a powerful tool to improve development efficiency Introduction to new features in PHP 8.3: a powerful tool to improve development efficiency Nov 27, 2023 am 10:03 AM

PHP has always been one of the most popular programming languages ​​for Internet development. Its simplicity, flexibility, and ease of learning and use make it the first choice of countless developers. In the latest version of PHP8.3, there are a series of new functions and improvements, which further improve development efficiency and user experience. This article will introduce some of the new features of PHP8.3 and discuss how they can become a weapon for developers. First, PHP8.3 introduces the new nullsafe operator. In the past, when we needed to access a pair

New features released in PHP8 that you must know New features released in PHP8 that you must know Jan 05, 2024 am 09:55 AM

PHP8 is released, you must know these new features! On December 3, 2020, PHP8 was officially released, bringing many exciting changes and new features to the majority of PHP developers. This article introduces some of the most important new features and provides corresponding code examples. JIT compiler In PHP8, a new JIT (Just-In-Time) compiler is introduced. The JIT compiler can directly compile PHP code into local machine code, thereby speeding up program execution. Here is an example using JIT

See all articles