A look at the best new features in Python 3.9

A look at the best new features in Python 3.9

It’s that time again: a new version of Python is imminent. Now in beta (3.9.0b3), we will soon be seeing the full release of Python 3.9.

Some of the newest features are incredibly exciting, and it will be amazing to see them used after release. We’ll cover the following ones:

  • Dictionary Union Operators
  • Type Hinting
  • Two New String Methods
  • New Python Parser

Let’s take a first look at these new features and how to use them.

Dictionary unions

One of my favorite new features with a sleek syntax. If we have two dictionaries a and b that we need to merge, we can now use the union operators.

We have the merge operator:

And the update operator |=, which updates the original dictionary:

If our dictionaries share a common key, the key-value pair in the second dictionary will be used:

Dictionary Update with Iterables

Another cool behavior of the |= operator is the ability to update the dictionary with new key-value pairs using an iterable object — like a list or generator:

If we attempt the same with the standard union operator | we will get a TypeError as it will only allow unions between dict types.

Image for post

Type hinting

Python is dynamically typed, meaning we don’t need to specify datatypes in our code.

This is okay, but sometimes it can be confusing, and suddenly Python’s flexibility becomes more of a nuisance than anything else.

Since 3.5, we could specify types, but it was pretty cumbersome. This update has truly changed that, let’s use an example:

Image for post