New Features in Python 3.9

Esteban Solorzano
Oct 9, 2020 · 3 min read

Python3.9 is here and brings with it some cool features that you should try now.

Image for post
Image for post
Photo by Hitesh Choudhary on Unsplash

Python is a programming language that has increased in popularity because it is easy to read and write; due to this is that each new version seeks ways to improve some of the complexities of how to perform certain operations to make our lives a little easier.

On October 5th was released the stable version of Python 3.9, which brings with it different features, some of which I will introduce below. I also invite you to install it and try it by yourself.

Installation

This installation is performed on Linux to install on another operating system check the Python page.

Union Operators in dict

Dictionaries are widely used data structures in Python, and there are different ways in which we can merge or update them; however, these options can be a bit messy:

There are more ways we could perform these operations; however, these are the ones that I would consider the most common, the first one, and because the update method modifies the dictionary, we should create a copy with the copy() method.

On the other hand, the way I think is the best way (so far) to merge two or more dictionaries requires that we have an understanding of how dictionary unpacking works to generate a new dictionary.

According to PEP 584, we can find two operators that we can use in a cleaner and better way:

These operators work the same way as the previous example, update dictionaries (|=), and merge two or more dictionaries in a new one (|).

New String Methods

Python3.9 includes two new strings methods that allow you to remove prefixes or suffixes. Before looking at how you can use these new methods, let’s see how you could do this in previous versions.

These operations are very simple; however, they do not correctly eliminate what we need since it was never validated whether or not it had the prefix or suffix.

The .removeprefix and .removesuffix methods not only take care of removing the information at the beginning or end of a string but before doing so, it checks that it contains the prefix or suffix according to what needs to be removed.

See PEP 616 for full description

Type Hinting Generics In Standard Collections

Because Python is dynamically typed, it can be sometimes confusing to recognize what type of data we are using in our code, that’s where we make use of type hints, which help us understand how our code should work, it is important to emphasize that the type hints will not raise an exception in case of giving a data type that does not match.

As you can see some of the type annotations must be imported from typing in their capitalized form, for example (List or Dict).

Now you can use the built-in types like list or dict without importing any annotations.

See PEP 585 for more details.

New Module zoneinfo

The datetime module allows us to work with dates and times in different ways; however, when it is time to work with time zones we must use third-party libraries to make use of them. PEP 615 details the motivation and proposal that led to the inclusion of this new module which allows us to define time zones based on the IANA database.

Also, the module provides a way to display each of the time zones in the IANA database.

Improvements to existing modules

There have been small improvements in different modules that are currently in the standard python library as:

Conclusions

Probably you have never needed any of these features; however, this is a brief introduction to what I think could be the most used in daily life and somehow allow our code to be more and more pythonic.

If you want to know what else this new version has to offer, take a look at the following link.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Esteban Solorzano

Written by

Software developer with passion for technology and development. https://estebansolorzano.com

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Esteban Solorzano

Written by

Software developer with passion for technology and development. https://estebansolorzano.com

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store