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… This story continues at The Next Web

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
No type hinting (left) v type hinting with 3.9 (right)

In our add_int function, we clearly want to add the same number to itself (for some mysterious undefined reason). But our editor doesn’t know that, and it is perfectly okay to add two strings together using + — so no warning is given.

What we can now do is specify the expected input type as int. Using this, our editor picks up on the problem immediately.

We can get pretty specific about the types included too, for example:

Image for post

Type hinting can be used everywhere — and thanks to the new syntax, it now looks much cleaner:

Image for post
We specify sum_dict’s argument as a dict and the returned value as an int. During the test definition, we also determine its type.
Live Updates for COVID-19 CASES