Python Is a well received programming language , Each version of the update will have a certain impact on the development community . recently ,Python 3.8 Entered beta 2
Version of the test , The new features have been added , The final version is expected this year 10 Monthly release .

Readers who work in the computer field Python
Programming languages should be very familiar . This is a well received dynamic programming language , Its flexibility and simple syntax make this language widely used in scripting tools , Data analysis ,Web The back end has a wide range of applications .Python
The development community is also very active ,3.x The iteration speed of the version of is very fast .2018 year 6 end of month ,Python 3.7 Coming out , after Python 3.8
The development and testing work of the system has also started . recently ,Python The software foundation made it public 3.80b2 Description document for , It was shown to the public beta Development of version testing , as well as Python 3.8
New features and functional improvements of the version .

at present ,Python 3.8 Of beta The testing process is in progress . this year 6 At the beginning of the month , The first one was officially released beta edition ――Python
3.80b1, It was released a month later Python 3.80b2. the second beta After release ,Python 3.8
New features have been added . The official has announced the release time of the final version , It's expected to be in the middle of this year 10 month .

text

Python 3.8 yes Python The latest version of the language , It is suitable for scripting , Automation and machine learning and Web Development and other tasks .

Now? Python 3.8 Has entered the official beta stage , This version brings a lot of syntax changes , Memory sharing , More efficient serialization and deserialization , Improved dictionary and more new features .

Python 3.8 Many performance improvements have also been introduced . in general , We are about to have a faster one , More accurate , More consistent and modern Python.

Here's what's going on Python 3.8 New features and most important changes , I've summed up some of the tools I've used in development , Common features .

1. Assignment expression

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

In the above example , If variable line If it does not exist, it will be created , And then file.readline() Is assigned to it . Then check line Is it "end". If not , Then read the next line , Save in line in , Then continue with the test .

The assignment expression follows the Python A tradition of simplicity , It's like a list parser . The aim is to avoid being in a particular environment Python There are some boring template code in programming mode . for example , The above code needs to write two more lines of code .

2. Parameters specified only by location

Parameters specified only by location are a new syntax in function definitions , You can let the programmer force a parameter to be specified only by location . This can solve the problem Python Which parameter in the function definition is a positional parameter , Which parameter is the fuzziness of the keyword parameter .

Only parameters specified by location can be used in the following cases : A function takes any keyword argument , But it can also accept one or more unknown parameters .Python This is usually the case with the built-in functions of , So programmers are allowed to do this , Can enhance Python Consistency of language .

Python The examples given in the document are as follows :

Symbol /
The positional parameter and keyword parameter are separated . In this case , All parameters are unknown . In previous versions of Python in ,z Will be considered as a keyword parameter . But using the above function definition ,pow(2,
10) and pow(2, 10, 5) It's the right way to call , and pow(2, 10, z=5) It's not true .

3. support f String debugging

f The string format makes it easier to output text and evaluate values or variables in the same expression , And it's more efficient .

output 4.

stay f String expression not added = Can output f The value of the expression itself , This is followed by the calculated value

The output is x+1=4.

4. Multi process shared memory

stay Python 3.8 in ,multiprocessing The module provides SharedMemory class , Can be in different Python Create shared memory area between cities .

In the old version of Python in , Shared data between processes can only be written to a file , Send via network socket , Or adopt Python Of pickle Module serialization and so on . Shared memory provides a faster way to transfer data between processes , So that Python More efficient multiprocessor and multi-core programming .

Shared memory fragments can be allocated as pure byte regions , It can also be assigned as a list like object that cannot be modified , It can save the number type , character string , Byte object ,None Objects, etc Python object .

5.Typing Module improvement

Python Is a dynamically typed language , But it can be done through typing Module add type prompt , For verification by third party tools Python code .Python
3.8 to typing Some new elements have been added , So it can support more robust checking :

final Modifiers and Final Type annotation indicates that , Objects that are decorated or marked should not be rewritten at any time , inherit , It can't be reassigned .

Literal Type restricts an expression to a specific value or list of values ( Not necessarily of the same type ).

TypedDict Can be used to create dictionaries , The value of its specific key is restricted to one or more types . Note that these restrictions are only used to determine the validity of values at compile time , It cannot be restricted at run time .

6. New version of pickle agreement

Python Of pickle Module provides a way to serialize and deserialize Python Method of data structure or instance , You can save the dictionary as it is for later reading . Different versions of Python Supportive pickle The agreement is different , The latest version has a wider range of support , Stronger , More efficient serialization .

Python
3.8 Introduction of the second 5 edition pickle Protocol can be used in a new way pickle object , It can support Python Buffer protocol for , as bytes,memoryviews or Numpy
array etc . new pickle Avoid a lot of problems in the pickle These objects are used as memory copy operations .

NumPy,Apache Arrow And other external libraries in their respective Python Support for new pickle agreement . new pickle It can also be used as Python
3.6 and 3.7 Plug in usage of , From PyPI Install on .

7. Reversible dictionary

Python3.6 The dictionary was rewritten in , It uses the PyPy A new realization of project contribution . Except faster , More compact , Today's dictionaries also inherit the order of elements ―― The elements are arranged in the order they were added , It's like a list .Python
3.8 It is also allowed to be used in dictionaries reversed().

8. Performance improvement

Many built-in methods and functions are faster 20%~50%, Because many functions need unnecessary parameter conversion before .

A new one opcode Caching can speed up specific instructions in the interpreter . however , At present, the only way to improve the speed is to improve the speed LOAD_GLOBAL
opcode, Its speed has increased 40%. Similar optimizations will be performed in future versions .

File copy operations such as shutil.copyfile() and shutil.copytree() Now use platform specific calls and other optimizations , To speed up the operation .

The newly created list is now smaller on average than before 12%, This is due to the optimization of the list constructor if the length of the list is known in advance .

Python 3.8 Middle new class ( as class
A(object)) Write operations in class variables become faster .operator.itemgetter() and collections.namedtuple() Speed optimization is also achieved .

9.Python C API and CPython realization

Python The latest version is in CPython(C Language writing Python Reference implementation of ) Used in C
API A lot of work has been done on refactoring . So far, these jobs are being added , Existing results include :

Python Initialization configuration (Python Initialization Configuration) There's a new one C
API, It can be realized to Python Initialize routines for tighter control and more detailed feedback . therefore , take Python It's easier to embed the runtime into other applications , It can also be programmed Python Program passing start parameters . new API It also ensures that all Python Configuration control has a single , Consistent position , So the future changes ( as Python The new UTF-8 pattern ) It's also easier .

CPython Another new one C
API――"vectorcall" Calling protocol ―― Can be achieved for Python Faster calling of internal methods , There is no need to create temporary objects . The API It's still unstable , But there has been a significant improvement . The API The plan is in Python
3.9 Medium maturity .

Python The runtime audit hook is Python The runtime provides two options API, Can be used to hook events , So as to ensure the test framework , External tools such as logs and auditing systems can monitor them .

10. How to download Python 3.8

This is about Python 3.8 New features ( Most people don't know ) That's all for the article , More relevant Python 3.8
Please search the previous articles of script home or continue to browse the related articles below. I hope you can support script home more in the future !

Technology