The best new features added to Python 3.8

Python 3.8 is the latest version of Python that has been released in beta and comes with various features. These features include syntax changes, memory sharing, optimization of serialization and deserialization operations, modified dictionaries, and more.

Naturally, users expected Python 3.8 to come with an optimized performance structure, which I must say can be seen in the end result. Python 3.8 is relatively faster and more compatible than its predecessors.

Assignment Expressions

One of the most noticeable changes that can be seen in Python 3.8 is the attribution expressions known by the =: operator. This operator helps us assign a value to a variable in another expression – it does its job even if the variable is not created. We used to use a separate line of code to assign a value to a variable, but now we can do the same thing in nested expressions.

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

In the example above, the line variable, even if it does not exist, is created from scratch and is set to file.readline (). The line value is then checked with “end”, if these values ​​are not equal the next line is executed.

The main idea behind this is to be able to do the assignment process with less coding. But in the end, no new feature has been added to the code, it can only be described as a shortcut.

Situational parameters only

In the new Python syntax, positional parameters cause a set of arguments to be called only positionally. This removes the ambiguity of which argument is situational and which is key.

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

In this example, the (/) parameter separates the situational and key arguments. In this case, all input arguments are called locally, but in previous versions of Python z would be recognized as a key argument. Therefore, in the above example, if we call the function as pow (2,10) or pow (2,10,5), there will be no problem, but if we call pow (2,10, z = 5), the program for We return the error.

F-string debugging support

The F-string format helps us to print a string with a combination of variables in a simpler way. Now in Python 3.8 we can see other functions in this format:

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

This shows output 4.

But if we put an equal sign at the end of it, the phrase itself will be printed. for example:

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

The output of this command will be x + 1 = 4.

Multiprocessing shared memory

In Python 3.8, the multiprocessing module introduces a new class called SharedMemory, which allows a new piece of memory to be created and shared between different Python processes.

In previous versions of Python, data could only be shared between processes in the file write operation, socket programming, and serialization operations using the pickle module. By sharing memory, data processing can be done much faster than before.

Improvements in typing module

Python is a dynamic programming language, but using the typing module allows us to implement similar modes to static languages. Python 3.8 has added new variants to the library, making the review operation much more powerful.

Among these new types, we can mention the final decorator and the Final type, which make it impossible for a decorator to override.

The Literal type also limits expressions to a specific value.

TypeDict allows you to limit the value of a key in a dictionary to a specific type.

New version of the pickle protocol

The Python pickle module gives you a way to Serialize and Deserialize Python data structures. Different versions of Python come with different levels of the pickle protocol, each with different capabilities for the serialization process.

Modifiable dictionaries

Dictionaries in Python version 3.6 were written entirely from scratch. A new version of this implementation was created with the participation of the PyPy project. The whole point of this new structure was to achieve greater speed and efficiency. Now in Python 3.8, a new method called reversed () has been added that can be applied to dictionaries.

Performance improvements

  • Many functions and methods run between 20 and 50 percent faster than previous modes. This was done by deleting a series of conversion operations.
  • Python speed has been increased in this version through a series of opcodes. In this version, LOAD_GLOBAL is considered, but the program is such that in the next versions of Python, other options will be considered as well.
  • File copy operations such as shutil.copyfile () and shutil.copytree () are now performed with greater speed and optimization.
  • The new Python lists are 12 percent smaller than the previous implementation. This has been done with high optimizations that have been done.
  • Writing on class properties is much faster now.
  • operator.itemgetter () and collections.namedtuple () have received improvements over the past.

Cpython and Python C API improvements

In recent versions of Python, most of the work has been done by modifying the C APIs used in Cpython. So far, the work has been positive and has brought with it new things, but apart from that, the following can also be considered:

A new C API for Python Initialization Configuration. With this feature, we want more complete control over Python initialization routines. This allows us to use Python running environment in different applications more quickly and easily.
The vectorcall call protocol is another new feature related to this classification, which is unstable in version 3.8, but is supposed to be a stable feature in Python 3.9.
Python runtime audit hooks are a new feature that provides two APIs for the Python runtime environment.

Leave a Reply

Your email address will not be published. Required fields are marked *