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

Website Content Writing Services in Mumbai: Picking the Top 7

India has its first “unicorn”, Neysa, from Mumbai in 2026. It reflects that the Mumbai… Read More

4 days ago

Top Quality Trailers Canning Vale for All Your Transport Needs

Are you in the market for a reliable trailer but feeling overwhelmed by all the… Read More

1 week ago

Why is Solar Energy a Popular Choice for Kolkata Businesses?

When you are running a business in Kolkata, it is very obvious that some costs… Read More

1 week ago

Top 10 Worst Foods to Avoid that Damage Your Teeth Enamel

A tooth is precious, and you need to keep it protected from harmful bacteria that… Read More

2 weeks ago

How to Choose the Right Asphalt Contractor for Your Project

Ever found yourself staring at a cracked driveway, wondering where on earth to start? Or… Read More

3 weeks ago

Betvictor login guide for Irish players – registration, 2FA, mobile access & bonuses

Getting Started: Registration and Your First Betvictor LoginCreating Your Betvictor AccountVerifying Your IdentityNavigating the Betvictor… Read More

3 weeks ago