Python is an open source, high level and object-oriented programming language, which is widely used in computer industries due to its versatility, ever-growing community, and continuous development, which in turn results in release of new and updated libraries, that are very essential for complex tasks. Hence, python has become the most preferred programming language among the developers in the recent years.

Python is a multipurpose language used for various tasks, such as web development, ethical hacking, data science and related fields like machine learning and artificial intelligence etc.

Since the first release of Python, it has been undergoing continuous upgrades. As a result, multiple versions have been launched so far with updates features, bug fixes etc. The latest release of Python 3.8.5 has come up with some significant improvements and updates, and also witnessed some new features such as:

•    Assignment expressions
There is new syntax := that assigns values to variables as part of a larger expression. It is affectionately known as “the walrus operator” due to its resemblance to the eyes and tusks of a walrus.
In this example, the assignment expression helps avoid calling len() twice:

if (n := len(a)) > 10:

   print(f"List is too long ({n} elements, expected <= 10)")

A similar benefit arises during regular expression matching where match objects are needed twice, once to test whether a match occurred and another to extract a subgroup:


discount = 0.0
if (mo := re.search(r'(\d+)% discount', advertisement)):
    discount = float(mo.group(1)) / 100.0

 

The operator is also useful with while-loops that compute a value to test loop termination and then need that same value again in the body of the loop:


#Loop over fixed length blocks
while (block := f.read(256)) != '':
    process(block)

 

•    Positional-only parameters
There is a new function parameter syntax / to indicate that some function parameters must be specified positionally and cannot be used as keyword arguments. This is the same notation shown by help() for C functions annotated with Larry Hastings’ Argument Clinic tool.
In the following example, parameters a and b are positional-only, while c or d can be positional or keyword, and e or f are required to be keywords:

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


The following is a valid call:


f(10, 20, 30, d=40, e=50, f=60)


However, these are invalid calls:


f(10, b=20, c=30, d=40, e=50, f=60)   # b cannot be a keyword argument
f(10, 20, 30, 40, 50, f=60)                   # e must be a keyword argument


One use case for this notation is that it allows pure Python functions to fully emulate behaviors of existing C coded functions. For example, the built-in divmod() function does not accept keyword arguments:
 

def divmod(a, b, /):
    "Emulate the built in divmod() function"
    return (a // b, a % b)


•    Parallel filesystem cache for compiled bytecode files
The new PYTHONPYCACHEPREFIX setting (also available as -X pycache_prefix) configures the implicit bytecode cache to use a separate parallel filesystem tree, rather than the default __pycache__ subdirectories within each source directory.
The location of the cache is reported in sys.pycache_prefix (None indicates the default location in __pycache__ subdirectories).


•    Debug build uses the same ABI as release build
Python now uses the same ABI whether it’s built in release or debug mode. On Unix, when Python is built in debug mode, it is now possible to load C extensions built in release mode and C extensions built using the stable ABI.


•    f-strings support = for self-documenting expressions and debugging
Added an = specifier to f-strings. An f-string such as f'{expr=}' will expand to the text of the expression, an equal sign, then the representation of the evaluated expression. For example:

 

>>> user = 'eric_idle'
>>> member_since = date(1975, 7, 31)
>>> f'{user=} {member_since=}'
"user='eric_idle' member_since=datetime.date(1975, 7, 31)"

 


•    Python Runtime Audit Hooks

The PEP adds an Audit Hook and Verified Open Hook. Both are available from Python and native code, allowing applications and frameworks written in pure Python code to take advantage of extra notifications, while also allowing embedders or system administrators to deploy builds of Python where auditing is always enabled.


List being almost endless, many other crucial features and updates are introduced with the release of Python 3.8.5 version which would surely be beneficial for developers.
So, these are some of the new updates in the recent version of Python, which are surely going to help the programmers to try out something different. I hope this article will help you out if you are interested in knowing about the recent developments in features of Python, and are looking forward to use it.

Written By: 

Amit Kumar
B.Tech - ECE
Birla Institute of Technology, Mesra