Take a Look at the Awesome New Features Coming in Python 3.9

The dictionary union operator, generic type annotations, and much more

Michael Krasnov
Jul 15 · 3 min read
Image for post
Image for post
Photo by Fabian Grohs on Unsplash

Python 3.9 is currently in the fourth iteration of its beta phase (as of July 2020). While some things may change, it is now pretty clear what the new release will look like.


Generic Type Annotations

Generic type annotations, AKA PEP 585, is something that many Pythonists were craving for a long time. Now, you can specify a collection and its item’s type without relying on the types library:

def do_stuff(data: list[int]): 
pass

Here are some collections that are now generalizable:

  • tuple
  • list
  • dict
  • set
  • frozenset
  • type
  • collections.deque
  • collections.abc.Coroutine
  • re.Pattern
  • and many more

Dict Union Operator

Image for post
Image for post
JS developers right now.

Before Python 3.9, the standard ways to merge to dicts were:

  1. The .update method: a.update(b)
  2. The spread operator (hello JS): {**a, **b}

Well, we are getting another one. The | union operator does exactly what it is supposed to do: Aunion of 2 dicts:

a = {'cars': 5, 'phones': 2} 
b = {'cows': 10, 'lizards': 3}
a | b
// {'cars': 5, 'phones': 2, 'cows': 10, 'lizards': 3}

Just like .update or the spread operator, this may result in loss of data if there are duplicate keys in the dict. Additionally, this operator works equally well with the augmented assignment:

a = {'cars': 5, 'phones': 2} 
b = {'cows': 10, 'lizards': 3}
a |= b
print(a)
// {'cars': 5, 'phones': 2, 'cows': 10, 'lizards': 3}

Remove Suffixes and Prefixes

New .removesuffix() and .removeprefix() string methods do just that:

'SubaruImpreza`.removeprefix('Subaru') // 'Impreza' 'SubaruImpreza'.removesuffix('Impreza') // 'Subaru'

New Parser

In Python 3.9, a new parser is used. It is based on PEG (Parsing expression grammar), unlike the old one, which uses LL(Left-to-right parser). This was done because the capabilities of the LL parser were depleted, and it was becoming increasingly hard to implement new language features. Most likely, you will notice no difference — the performance is comparable, and full backwards compatibility is validated.

You need to care about it only if your code uses the parser module from the standard library. It was deprecated a while ago and will not work with the new parser. As a workaround, you can switch back to old parser using the argument -X oldparser or an environment variable PYTHONOLDPARSER=1.


Zoneinfo Module

Python 3.9 introduced a new module called zoneinfo. It implements the ZoneInfo class, which provides support for the IANA time zones.

from zoneinfo import ZoneInfo 
from datetime import datetime, timedelta
timestamp = datetime(2020, 7, 15, 11, tzinfo=ZoneInfo("America/Los_Angeles"))

Graphlib Module

Another new module — this time for dealing with graphs. Right now, it only implements the topological sorting algorithm for directed acyclic graphs. If you did not understand what I just wrote, you most likely won’t require the new module. For those of you who are familiar with the graph theory, this is how the module should be used (taken from Python docs):

>>> graph = {"D": {"B", "C"}, "C": {"A"}, "B": {"A"}} 
>>> ts = TopologicalSorter(graph)
>>> tuple(ts.static_order())
('A', 'C', 'B', 'D')

Closing notes

Thank you for reading, I hope you liked my article. Stay subscribed for more to come!

Resources

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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