Python 3.8 is the latest version of the Python language, which is suitable for scripting, automation, and a variety of tasks such as machine learning and web development. Python 3.8 is now in its official beta stage and this version brings many syntax changes, memory sharing, more efficient serialization and deserialization, improved dictionaries and many more new features.
Python 3.8 also introduces many performance improvements. Overall, we're about to have a faster, more accurate, more consistent, and more modern Python.Here's what's new and what's changed most in Python 3.8.
1. Assignment expressions
The most obvious change in Python 3.8 is the assignment expression, the := operator. An assignment expression can be used to assign a value to a variable, even if the variable doesn't exist. It can be used in an expression without having to appear as a separate statement.
while (line := ()) != "end": print(chunk)"end": print(chunk)
In the above example, the variable line is created if it does not exist, and the return value of () is assigned to it. Then check if line is "end". If not, the next line is read, saved in line, and the test continues.
Assignment expressions follow the tradition of Python's usual simplicity, just like list parsing. Its purpose is to avoid some boring sample code in a particular Python programming model. For example, the above code would require two extra lines of code in the normal way of writing.
2. Parameters specified only by position
Arguments Specified by Position Only is a new syntax in function definitions that allows programmers to force a particular argument to be specified by position only. This resolves the ambiguity in Python function definitions as to which parameter is a positional parameter and which is a keyword parameter.
Arguments specified only by position can be used in situations where a function accepts any keyword argument, but can also accept one or more unknown arguments.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:
def pow(x, y, z=None, /): r = x**y if z is not None: r %= z return r r = x**y if z is not None: r %= z return r
The symbol / separates the positional and keyword parameters. In this example, all parameters are unknown parameters. In previous versions of Python, z would have been considered a keyword argument. However, using the function definitions above, pow(2, 10) and pow(2, 10, 5) are both correct calls, while pow(2, 10, z=5) is incorrect.
3、Support f string debugging
The f string format makes it easier and more efficient to perform calculations on output text and values or variables within the same expression.
x = 3 print(f'{x+1}') print(f'{x+1}')
Output 4.
Adding = to the end of an f-string expression outputs the value of the f-expression itself, followed by the computed value.
x = 3print (f'{x+1=}') print (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 creates a shared memory area between different Python progressions.
In older versions of Python, sharing data between processes could only be done by writing to a file, sending it over a network socket, or serializing it using Python's pickle module. Shared memory provides a faster way to pass data between processes, thus making multi-processor and multi-core programming in Python more efficient.
Shared memory fragments can be allocated as mere byte regions or as unmodifiable list-like objects that can hold a small number of Python objects such as numeric types, strings, byte objects, None objects, and so on.
5. Improvements to the Typing module
Python is a dynamically typed language, but type hints can be added via the typing module to allow third-party tools to validate Python code.Python 3.8 adds some new elements to typing so it can support more robust checking:
- The final modifier and Final type annotation indicate that the modified or annotated object should not be overridden, inherited, or reassigned at any time.
- The Literal type limits an 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, not at run time.
6. New version of the pickle protocol
Python's pickle module provides a way to serialize and deserialize Python data structures or instances, saving the dictionary as is for later reading. Different versions of Python support different pickle protocols, and the latest version supports a broader, more powerful, and more efficient serialization.
The version 5 pickle protocol introduced in Python 3.8 allows to pickle objects in a new way that supports Python's buffer protocols such as bytes, memoryviews, or Numpy array. The new pickle avoids many of the memory copying operations that occur when pickling these objects.
External libraries such as NumPy, Apache Arrow and others 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
Dictionaries were 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 elements - elements are listed in the order they were added, just like a list.Python 3.8 also allows reversed() on dictionaries.
8. Performance improvements
- Many of the built-in methods and functions are 20% to 50% faster, as many of them previously required unnecessary parameter conversions.
- A new opcode cache can improve the speed of specific instructions in the interpreter. However, the only speed improvement currently realized is the LOAD_GLOBAL opcode, which is 40% faster. Similar optimizations will be made in future releases.
- File copy operations such as () and () 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 that the list constructor would have made if it had known the length of the list ahead of time.
- Writes to class variables of new types of classes, such as class A(object), have become faster in Python 3.8. () and () have also been optimized for speed.
9. Python C API and CPython implementation
Recent versions of Python have put a lot of work into refactoring the C API used in CPython, the reference implementation of Python written in C. This has been done in a number of ways. So far these efforts are still being added to, and existing results include:
- Python Initialization Configuration has a new C API that enables tighter control and more detailed feedback on Python initialization routines. 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 there is a single, consistent location for all Python configuration controls, so that later changes (such as Python's new UTF-8 mode) are easier.
- Another new C API for CPython - the "vectorcall" calling protocol - enables faster calls to Python's internal methods without creating temporary objects. The API is still unstable, but there have been significant improvements. The API is scheduled to mature in Python 3.9.
- Audit hooks for the Python runtime provides two APIs for the Python runtime that can be used to hook events so that external tools such as test frameworks, logging and auditing systems can monitor them.
10. How to download Python 3.8
Please click the link below to download Python 3.8 beta from the Python Software Foundation:
/downloads/release/python-380b1/
Original: /article/3400640/?upd=1560521475943
This is the whole content of this article.