What’s new in Python 3.8? – Unveiling the Latest Features of Python

Free Python courses with 57 real-time projects - Learn Python

Python has officially released its latest version Python 3.8 on 14th October 2019. This release has rolled out some great new features for all the Python programmers.

Python 3.8 is faster than ever with some major improvements in its performance.

In this Python 3.8 tutorial, let’s see what has been changed from its previous version 3.7.

Python 3.8 Features

whats new in python 3.8 - Python 3.8 Features

Now, we will quickly dive into the latest features of Python 3.8:

1. PEP 572 (Assignment Expressions) – The walrus operator (:=)

You will get to see a new type of operator which is being known as the walrus operator (:=).

This allows you to assign variables inside an expression. The major benefit of this is to save you some lines of code and you can write even cleaner and compact code in Python.

Example of Walrus Operator in Python

num = [1,2,3,4,5]
if( (size:=len(num)) < 10 ):
  print(f”Length of list is small, size={size}”)

Example 2

This can be useful while writing loops, something like this.

Item = getItem()
while Item:
  do_something(item)
  Item = getItem()

Can be written as:

while Item:= getItem():
  do_something(Item)

Example 3

Simplifying list comprehensions when filtering a value

[y for x in data if (y := f(x)) is not None]

2. PEP 570 (Positional only arguments)

There is a new function parameter syntax (/) to highlight that some of the functions must be stated positionally and not by keyword arguments.

We also have an operator (*) that indicates that the arguments must be keyword only.

This can be a little confusing but after seeing the code this gets easier to understand, so let’s see them in action.

def func(a, b, c, d):
  print(a,b,c,d)

We can call this function however we want with keyword arguments or positions.

func(d=2, a=3, b=2, c=6) #Valid - prints 3 2 6 2
func(1,3,4,5) #Valid - prints 1 2 4 5

Now consider the following definition:

def func( a,b,/,c,d,*,e,f ):
  print(a,b,c,d,e,f )

This will impose the way we can call this function:

  • a and b arguments are positional only.
  • c and d arguments can be positional as well as keyword.
  • e and f arguments are keyword only.
func(1,2, 3,4, e=5, f=6 ) #Valid - prints 1 2 3 4 5 6
func(1,2, c=3,d=4, e=5, f=6 ) #Valid - prints 1 2 3 4 5 6

However, these calls will be invalid

func(a=1,b=2, 3,4, e=5, f=6 ) #Invalid - Error : a and b arguments are positional only 
func(1,2, c=3,d=4, 5, 6 ) #Invalid - Error : e and f are keyword only arguments.

3. PEP 590 ( Vectorcall)

This release has made some improvements in the vector call which is a fast calling protocol for CPython.

A new C API is introduced to optimize the calls of objects.

This feature was already used in CPython but with the new C API, “fastcall” convention can be used by a user-defined extension class.

4. PEP 574 ( Pickle Protocol 5 with out-of-band data)

Pickle is useful to transfer big amounts of data between Python processors to take full advantage of multicore processors.

It’s important to maximize the transfer speed by optimizing memory copies. Pickle protocol 5 now supports out-of-band data buffers and extra metadata is required.

  • PickleBuffer type for __reduce_ex__ returns out-of-band buffers.
  • buffer_callback parameter while pickling handles out-of-band data buffers.
  • buffers parameter while unpickling shows out-of-band data buffers.

5. F-strings now support = (Easy debugging)

A small improvement has been made in the f-strings formatting. They can now support = operator in f-strings that allows debugging easier.

In previous Python versions, this would give you Syntax Error:

A = 5
print(f’{A=}’)

Now from Python 3.8, we can do the following

A=5
print(f”{A=}”) #prints : A=5

6. Improved Typing

6.1 PEP 591 ( Final qualifier )

Python now supports the “final”. Java programmers already know about this. It has 3 major uses:

  • Declaring a class final will prevent it from inheriting.
  • Declaring a variable final will prevent it from reassigning the value.
  • Declaring a method final will prevent it from being overridden.

6.2 PEP 586 (Literal types )

Literal types are useful to know the literal value of an attribute or a variable. They are useful in type checking.

Consider this expression:

0== False

This will give True as a result but 0 is of type integer and False is of type bool. So with literal, we can force type checks to be literally some specific type.

Python 3.8 Interview Questions

  1. What is Walrus Operator in Python?
  2. What is Python 3.8 6rc1?
  3. Tell some of the features in Python 3.8?
  4. What are the uses of Final Qualifier in Python?
  5. Compare Python 2 vs Python 3.

Summary

These are all the new features in Python 3.8.

It is safe to upgrade to python 3.8 and start experimenting with new features like assignment expressions and positional-only arguments.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

3 Responses

  1. Dihfahsih Mugoya says:

    I hope all the libraries in 3.7 where included in this 3.8 because they are super cool

    • VB says:

      num = [1,2,3,4,5]
      if( (size:=len(num1)) < 10 ):
      print(f”Length of list is small, size={size}”)

      I think its not num1, its num. please correct me if i am wrong

      thank you

Leave a Reply

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