All Articles

Some Cool New Features of Python 3.9

Invoke AWS Lambda from AWS Step Functions with Terraform

Photo by Setyaki Irham on Unsplash

Python 3.9 is released on 5th October 2020 and is now available. It comes with some of the great features and lots of improvements. In this article, we will explore some of its cool features.

To know more about what is coming with Python 3.9 visit this page. And if you wish to see the full changelog then go here.

Dictionary Merge & Update Operators


Two new operator Merge (|) and Update (|=) are added to the built-in class dict.
Merge (|) creates a new dictionary with the merged keys and values. It complements {**d1, **d2} methods of merging two dictionaries.

>>> d1 = {"a": 1}
>>> d2 = {"a": 3, "b": 2}
>>> d1 | d2
{'a': 3, 'b': 2}

If d1 and d2 shares common keys then d2 will get preference over d1.

The Update (|=) operator will update d1 in-place like the update method of dict.

>>> d1 = {"a": 1}
>>> d2 = {"a": 3, "b": 2}
>>> d1 |= d2
>>> d1
{'a': 3, 'b': 2}

Like the Merge (|) operator in Update (|=) operator d2 will get preference over d1 if they share common keys.

New String Methods to Remove Prefixes and Suffixes


Two new string methods str.removeprefix(prefix) and str.removesuffix(suffix) are added in Python 3.9. These two methods remove unnecessary prefix or suffix from a string.

>>> "The Python 3.9".removeprefix("The ")
'Python 3.9'
>>> "Python 3.9 is released".removesuffix(" is released")
'Python 3.9'

If the string is not started with prefix or ends with suffix then the original string will get returned.

Type Hinting Generics in Standard Collections


From now on we can use built-in collection types such as list or dict as generic types in the type annotations. So we don’t have to import capitalized types (List or Dict) from typing module.

def print_messages(messages: list[str]) -> None:
    for message in messages:
        print(message)

New Library Modules

  • IANA Time Zone Database in zoneinfo module

Python 3.9 comes with zoneinfo module that supports IANA time zone database. This module provides zoneinfo.ZoneInfo a concrete implementation to support IANA data. zoneinfo will use the system’s time zone data if available, otherwise, it will use data from first-party tzdata available on PyPI.

>>> from datetime import datetime
>>> from zoneinfo import ZoneInfo
>>> dt = datetime.now()
>>> print(dt)
2020-10-06 19:28:04.854942
>>> print(dt.astimezone(ZoneInfo('Europe/London')))
2020-10-06 14:28:04.854942+01:00

  • An implementation of topological sort of graph in graphlib module

graphlib is a new module that comes with Python 3.9 that contains the graphlib.TopologicalSorter class. This class has the functionality of performing the topological sort on a directed acyclic graph. graphlib.TopologicalSorter takes an optional parameter graph.

>>> from graphlib import TopologicalSorter
>>> graph = {"4": {"2", "3"}, "3": {"1"}, "2": {"1"}}
>>> t_sort = TopologicalSorter(graph)
>>> list(t_sort.static_order())
['1', '2', '3', '4']