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.
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:
#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.
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
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!

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)