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

Learn about the new features of Python 3.8 in one article

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼forward
2019-08-19 17:58:523482browse

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:
This article is reproduced at:jianshu.com. If there is any infringement, please contact admin@php.cn delete