Tech Reviews

Python 3.8 Overview

The release of Python 3.8 is scheduled for October 2019, but now everyone has the opportunity to feel a set of new features of the language. While writing this post, python version 3.8b2 is available on the official website .

So, what is the upcoming release preparing for us?

f-string =

Now displaying debug information has become even easier and more beautiful. It is enough to pass the = sign to f-string:

Assignment Expressions or: =, aka walrus operator

The legendary PEP572, due to which Guido stepped down as dictator of Python. A statement assigns a value to a variable in an expression. For example, you want to check if a key is in the dictionary, and also assign the value of this key to a variable. In versions of Python before 3.8, you need to do:

In version 3.8, the code looks a little more compact:

params = {'foo': 'bar'}
if z := params.get('foo'):
    print(z)

Positional-only arguments

In python 3, keyword-only arguments ( PEP3102 ) appeared back in 2006 . Their task is to enforce the passing of arguments to a function or method by key after the *

The interpreter tells us that the function accepts only 2 positional arguments, but 4 was passed in order to avoid swearing, the function must be called like this:

test(1, 2, key1=3, key2=4)

In python 3.8, it became possible to specify the transfer of arguments exclusively according to their position. Unlike keyword-only arguments, all arguments must be passed according to position:

def position_only(a, b, c, d=0, /):
    pass

position_only(1, 2, 3)  # OK

position_only(1, 2, c=3)  # This raises exception

Shared memory for IPC

For interprocessor communication, python 3.8 introduced shared memory. Previously, in order to transfer data between processes, the internal mechanism meant serializing and deserializing an object and sending data to a socket. With the use of the OS mechanism for allocating shared memory, there is no need to communicate through a disk or socket, and this gives a significant increase in performance. The shared_memory module has been added to the multiprocessing package . An example of using shared memory between two different processes:

from multiprocessing import shared_memory
a = shared_memory.ShareableList(range(5))

TypedDict

Now you can specify the type of values ​​for dictionaries with a fixed number of string keys. For instance:

from typing import TypedDict

class Movie(TypedDict):
    title: str
    year: int

movie: Movie = {
    'title': 'Movie title', 
    'year': 2020
}

The Movie class can be used in annotations:

from typing import List
def my_function(films: List[Movie]):
    pass

final

Those who are familiar with Java immediately understand what final is. What is marked final should not change during the life cycle of the script.

from typing import Final, final

birth_year: Final = 1989
birth_year = 1901  

Classes marked with the final decorator must not be inherited:

@final
class FinalClass:
    pass

class InheritedFromFinal(FinalClass): 
    pass

You can also formally prohibit overriding a method in a child class:

class MyClass:
    @final
    def prohibited(self):
        pass

class SecondClass(MyClass):
    def prohibited(self): 
        pass

These are not all the features in the new version of Python. More information about the new products can be found here .

shahidsidd

Recent Posts

Descoperă cele mai puternice strategii pentru câștiguri la Malina Casino Industria jocurilor de noroc a crescut exponenț

zk_ff8a7a59daea424ea5aa83649482ae4a Descoperă cele mai puternice strategii pentru câștiguri la Malina Casino Industria jocurilor de noroc… Read More

19 hours ago

Unlock the secrets to mastering casino strategy for big wins Understanding the intricacies of casino strategy can signif

zk_8f1d7fa5b8bc4e4986002ce821452346 Unlock the secrets to mastering casino strategy for big wins Understanding the intricacies of… Read More

22 hours ago

Why the Ford Barra Engine Is a Favourite Among Enthusiasts?

Discover why the Ford Barra engine is loved by enthusiasts for its reliability, tuning potential,… Read More

1 day ago

Unlock the secrets to winning big at the casino Whether you’re an experienced high roller or a curious newcomer, winning

zk_c9608782fdc041fb8c98061599431565 Unlock the secrets to winning big at the casino Whether you're an experienced high… Read More

1 day ago

Ontdek de beste strategieën voor winst in het casino In de wereld van het gokken is het casino een opwindende plek waar

zk_1c7b1e834c3f4bab9e680cf219a43c2f Ontdek de beste strategieën voor winst in het casino In de wereld van het… Read More

1 day ago

Discover the secrets to winning at online casinos Negli ultimi anni, i casinò online hanno guadagnato una popolarità str

zk_da69e41863e34013996dced94304ab81 Discover the secrets to winning at online casinos Negli ultimi anni, i casinò online… Read More

1 day ago