AI for Women

Python 3.8 : How Significant are the New Features?

Python 3.8.0 (beta 3) was released on the 29th of July, 2019. While some Python Developers agree that there are significant changes in Python 3.8, there are others who feel that the changes make no impact for a developer, especially for one in the field of Data Science. 

So, what’s to be expected for a Python programmer who is yet to try out the new version? According to the Python website, here are some of the new features that we will be seeing –

1. Positional Only Parameter:

Consider a simple addition Function,

def sum(a, b, c=None):
      if c != None:
      return x

This function will give us the value of x, based on the values of a, b, and c, specified by us. By default, c will have no value. However, if we do give it a value, it will be added to the final sum. The result would look something like this,

Case 1:
Input -> sum(1,2,3)
Output -> 6

We can also get the answer in this way,

Case 2:
Input -> sum(b=3, a=2)
Output -> 5

Thus, we find that we can enter the values based on the position (Case 1) or on the keyword (Case 2).

In Python 3.8, something called the Positional Only Parameter has been introduced, which enables us to specify whether the values of a function are to be entered based only on position, or also on keyword. This parameter is the ‘/’. So, with this Parameter, the function would look something like this,

def sum(a, b, c=None, /):
      if c != None:
      return x

Thus, we can enter the values as we did in Case 1 to get an answer, while if we entered the values as we did in Case 2, we would receive an error message.

2. Assignment Expressions:

The ‘:=’ operator, also known as the ‘Walrus Operator’, allows us to assign a value to a variable as a part of an Expression, without initially assigning any value to that variable.

Consider the example,

if (a:=2) is not None:

This ‘:=‘ operator will assign the value ‘2’ to the previously undefined variable ‘a’, and then print its value.

3. Pickle Protocol 5 with Out of Band Data:

First, let us understand what ‘Pickling’ is in Python. Pickling is used to serialise or deserialise a Python object structure. So, we can convert Python objects (like lists, dictionaries, etc.) into streams of characters.
Second, let us understand what ‘out of band’ data is. This is the type of data that is transferred through a stream, and which is independent from the main ‘inband’ stream of data.

Thus, there is a new Pickle version, Protocol 5, for handling this type of ‘out of band’ data.

4. Use of Pickle Protocol 4 by Default:

The use of Protocol 4 improves the overall performance. Protocol 4 features Binary Framing, Binary Encoding (for all opcodes), better String Encoding, and so on. Thus, making it the default protocol implies better results for our Python Program.

5. Runtime Audit Hooks:

This was introduced with the promise of increasing security and transparency. It aims to achieve this by allowing auditing and security tools to know when Python may be running operations that could prove to be potentially harmful.

The Runtime Audit Hook API will thus raise warning messages, based on particular types of Python operations.

6. Python Initialization Configuration:

Python is currently extremely configurable. However, this feature did evolve with time. The new update introduces a C API, which adds some new structures and functions, to give Python Developers better control over the initialisation configuration. It allows us to read the configuration, modify it, and then apply it to Python.

As a Python Developer, what do you think about these changes? Do you think they are beneficial, or, as some have said, are they insignificant?

Welcome to AI For Women