Open In App

Awesome New Features in Python 3.8

Improve
Improve
Like Article
Like
Save
Share
Report

Python is a widely-used general-purpose, high-level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. Python is a programming language that lets you work quickly and integrate systems more efficiently.
Python 3.8 contains many small improvements over the earlier versions. This article outlines the most significant additions and changes in python 3.8
 

Positional-only parameters(/)

There is a new function parameter syntax “/” which indicates that some function parameters must be specified positionally and can’t be used as keyword arguments. The addition of “/” improves the language’s consistency and allows a robust API design.
Example:
 

Python3




# Arguments before / are considered
# as positional arguments only
def add(x, y, /, z = 0):
    a = x + y + z
    return
 
# Driver's code
print(add(2, 5))
print(add(2, 5, 7))
print(add(x = 2, y = 5))


Output: 
 

7
14

Traceback (most recent call last): 
File “”, line 1, in 
TypeError: add() got some positional-only arguments passed as keyword arguments: ‘x, y’ 
 

The marker “/” means that passing values for x and y can only be done positionally, but not using keyword arguments.
 

Assignment Expressions(:=)

This operator is used to assign and return a value in the same expression. This removes the need for initializing the variable upfront. The major benefit of this is it saves some lines of code. It is also known as “The Walrus Operator” due to its similarity to the eyes and tusks of a walrus.
Example:
 

Python3




a = 20
if (b := a) > 10:
    print(f"The value of b is {b} and is greater than 10.")


Output:
 

The value of b is 20 and is greater than 10.

 

f-strings now support “=”

This string formatting mechanism is known as Literal String Interpolation or more commonly as F-strings (because of the leading f character preceding the string literal). The idea behind f-strings is to make string interpolation simpler. Python 3.8 allows the use of the above-discussed assignment operator and equal sign (=) inside the f-strings.
For example, say we have two variables “a” and “b”, and we want to print “a + b” along with the result. Here we can use f-strings=.
 

Example:

Python3




a = 5
b = 10
 
# Using = at the end of
# f-strings
print(f'{a + b = }')
 
# Using assignment operators
# inside f-strings
print(f'{(c := a + b)}')
 
print("The value of c:", c)


Output:
 

a + b = 15
15
The value of c: 15

This is very useful during debugging, especially for everyone who uses print() statements for debugging.
 

reversed() works with a dictionary

Unlike Python 3.7, now in Python 3.8, the built-in method “reversed()” can be used for accessing the elements in the reverse order of insertion.
Example:
 

Python3




# Declaring a dictionary
my_dict = dict(x = 1, y = 2, z = 3)
 
# Prints only keys
list(reversed(my_dict))
 
# Prints the key-value pair
# as a list of tuples
list(reversed(my_dict.items()))


Output:
 

['z', 'y', 'x']
[('z', 3), ('y', 2), ('x', 1)]

No parentheses for return and yield statements

‘yield’ and ‘return’ statements do not require parentheses to return multiple values.

For example:
 

Python3




def parse(family):
  lastname, *members = family.split()
  return lastname.upper(), *members
 
print(parse('Charles David John Sam'))


Output:

('CHARLES', 'David', 'John', 'Sam')

pow() function

In the three-argument form of pow(), when the exponent is -1, it calculates the modular multiplicative inverse of the given value.

For example, to compute the modular multiplicative inverse of 38 modulo 137:

Python3




print(pow(38, -1, 137))


Output:

119

Syntax Warning

If you miss a comma in your code such as a = [(1, 2) (3, 4)], instead of throwing TypeError, it displays an informative Syntax warning like this:

SyntaxWarning: 'tuple' object is not callable; perhaps you missed a comma?

Dictionary comprehension

Dict comprehensions have been modified so that the key is computed first and the value second:

>>> data = {input('Name: '), input('Age: ')}
Name: Charles
Age: 40

‘csv’ module

The ‘csv.DictReader’ now returns instances of dictionary instead of a ‘collections.OrderedDict’.

importlib_metadata

importlib_metadata is a new library added in the Python’s standard utility modules, that provides an API for accessing an installed package’s metadata, such as its entry points or its top-level name.
Example: The version of pip can be checked using this module. 
 

Python3




from importlib import metadata
 
# pip version
print(metadata.version('pip'))
data = metadata.metadata('pip')
print()
print(data)


Output:
 

9.0.1
Metadata-Version: 2.1 
Name: pip 
Version: 9.0.1 
Summary: The PyPA recommended tool for installing Python packages. 
Home-page: https://pip.pypa.io/ 
Author: The pip developers 
Author-email: python-virtualenv@groups.google.com 
License: MIT 
Description: pip 
=== 
The `PyPA recommended 
`_ 
tool for installing Python packages. 
* `Installation `_ 
* `Documentation `_ 
* `Changelog `_ 
* `Github Page `_ 
* `Issue Tracking `_ 
* `User mailing list `_ 
* `Dev mailing list `_ 
* User IRC: #pypa on Freenode. 
* Dev IRC: #pypa-dev on Freenode. 
.. image:: https://img.shields.io/pypi/v/pip.svg 
:target: https://pypi.python.org/pypi/pip 
.. image:: https://img.shields.io/travis/pypa/pip/master.svg 
:target: http://travis-ci.org/pypa/pip 
.. image:: https://img.shields.io/appveyor/ci/pypa/pip.svg 
:target: https://ci.appveyor.com/project/pypa/pip/history 
.. image:: https://readthedocs.org/projects/pip/badge/?version=stable 
:target: https://pip.pypa.io/en/stable 
Code of Conduct 
————— 
Everyone interacting in the pip project’s codebases, issue trackers, chat 
rooms, and mailing lists is expected to follow the `PyPA Code of Conduct`_. 
.. _PyPA Code of Conduct: https://www.pypa.io/en/latest/code-of-conduct/ 
Keywords: easy_install distutils setuptools egg virtualenv 
Platform: UNKNOWN 
Classifier: Development Status :: 5 – Production/Stable 
Classifier: Intended Audience :: Developers 
Classifier: License :: OSI Approved :: MIT License 
Classifier: Topic :: Software Development :: Build Tools 
Classifier: Programming Language :: Python :: 2 
Classifier: Programming Language :: Python :: 2.6 
Classifier: Programming Language :: Python :: 2.7 
Classifier: Programming Language :: Python :: 3 
Classifier: Programming Language :: Python :: 3.3 
Classifier: Programming Language :: Python :: 3.4 
Classifier: Programming Language :: Python :: 3.5 
Classifier: Programming Language :: Python :: Implementation :: PyPy 
Requires-Python: >=2.6,!=3.0.*,!=3.1.*,!=3.2.* 
Provides-Extra: testing 
 

 

Performance 

  • In Python 3.8, the multiprocessing module offers a SharedMemory class that allows regions of memory to be created and shared between different Python processes. Shared memory provides a much faster path for passing data between process, which eventually allows Python to use multiple processors and multiple cores more efficiently.
  • Many built-in methods and functions have been speed up by 20% to 50%.
  • Newly created lists are now on average 12% smaller than before
  • Writes to class variables are much faster in Python 3.8.

 



Last Updated : 29 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads