Secrets of the new features of Python 3.8

From the powerful new assignment syntax to the big changes at the bottom, Python 3.8 moves towards a more modern Python.

Python 3.8 is the latest version of the Python language, which is suitable for scripting, automation, and various tasks such as machine learning and web development. Now 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. In general, we are about to have a faster, more precise, more consistent and more modern Python. Here are the new features and most important changes in Python 3.8.

1、 Assignment expression

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

while(line := file.readline())!="end":print(chunk)

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 the line is "end". If not, read the next line, save it in line, and continue the test.

Assignment expressions follow Python’s tradition of simplicity, just like list comprehensions. The purpose is to avoid some boring boilerplate code in specific Python programming patterns. For example, the above code needs to write two more lines of code in general writing.

2、 Parameters specified by position only

The parameter specified by position only is a new syntax in the function definition, which allows the programmer to force a parameter to be specified only by position. This can solve the ambiguity of which parameter is a positional parameter and which parameter is a keyword parameter in the Python function definition.

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

The examples given in the Python documentation are as follows:

def pow(x, y, z=None,/):
 r = x**y
 if z is not None:
  r %= z
 return r

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, and pow(2, 10, z=5) is incorrect.

3、 Support f string debugging

The f string format makes it easier to calculate output text and values or variables in the same expression, and it is more efficient.

x =3print(f'{x+1}')

Output 4.

No = added at the end of the f string expression can output the value of the f expression itself, followed by the calculated value

x =3print(f'{x+1=}')

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 a shared memory area between different Python processes.

In the old version of Python, sharing data between processes can only be done by writing files, sending them through network sockets, or serializing them using Python's pickle module. Shared memory provides a faster way to transfer data between processes, making Python's multi-processor and multi-core programming more efficient.

The shared memory segment can be allocated as a pure byte area, or as an unmodifiable list-like object, which can store a small number of Python objects such as numeric types, strings, byte objects, and None objects.

5、 Improvement of Typing module

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

6、 New version of the pickle protocol

Python's pickle module provides a way to serialize and deserialize Python data structures or instances, and you can save the dictionary as it is for later reading. Different versions of Python support different pickle protocols, and the latest version supports a wider, more powerful, and more efficient serialization.

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

External libraries such as NumPy and Apache Arrow support the new pickle protocol in their Python bindings. The new pickle can also be used as a plugin for Python 3.6 and 3.7 and can be installed from PyPI.

7、 Reversible dictionary

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

8、 Performance improvement

9、 Python C API and CPython implementation

The most recent version of Python has put a lot of effort into refactoring the C API used in CPython (a reference implementation of Python written in C). So far these works are still being added, and the existing results include:

Recommended Posts

Secrets of the new features of Python 3.8
Some new features of Python 3.10
7 features of Python3.9
First look at the new features of Ubuntu 17.10
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
The usage of wheel in python
Python handles the 4 wheels of Chinese
Python simulation of the landlord deal
What is the use of Python
List of new features of Zabbix 5.0 LTS
The premise of Python string pooling
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Two days of learning the basics of Python
What is the id function of python
The essence of Python language: Itertools library
The specific method of python instantiation object
python3 realizes the function of mask drawing
What is the prospect of python development
What is the function body of python
The specific method of python import library
Solve the conflict of multiple versions of python
2.1 The Python Interpreter (python interpreter)
What is the function of adb in python
Detailed explanation of the principle of Python super() method
The difference between the syntax of java and python
Python realizes the development of student management system
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
Solve the problem of python running startup error
Can the value of the python dictionary be modified?
Python implements the source code of the snake game
Detailed explanation of the usage of Python decimal module
How about learning python at the age of 27?
The consequences of uninstalling python in ubuntu, very
Python writes the game implementation of fishing master
[898] python get the intersection of two lists | union | difference
Detailed explanation of the principle of Python function parameter classification
What is the advantage of python over corporate language
A brief summary of the difference between Python2 and Python3
Detailed explanation of the principle of Python timer thread pool
Solve the problem of python compiling and installing ssl
Detailed explanation of the implementation steps of Python interface development
Python crawls the full set of skins of the king pesticide
How to understand the introduction of packages in Python
Basics of Python syntax