New features in Python 3.8 & 3.9
At my job, we have just upgraded Python from 3.7 to 3.9, and I got super excited about all the new features. This is a blog post of highlights from these two releases.
This was a bit of a controversial feature during Python 3.8’s development and PEP 572 was even part of the reason Guido resigned as a benevolent dictator.
The new feature is pretty straightforward, here are some examples straight from the PEP:
I remember using the assignment expression only once in a personal project so far but I do feel it’s a nifty way to save a line of code here and there.
PEP 570 added positional-only arguments. These arguments have no externally-usable name and therefore cannot be called with kwargs. You would use them when you don’t want to expose the function parameter name so that changing it later won’t break anyone’s code.
The function definition has two positional-only arguments (
b), two parameters that can be both positional or keyword (
d) and two keyword only parameters (
Positional only-arguments were already used in the Python standard library and I like that they became a core feature. They might not be super interesting in our day-to-day, but library authors do appreciate them.
This is a feature that I use a lot when debugging. Appending the
= character to the f-string expression will print out the name of the variable used:
Very useful for debugging, but remember that using f-strings isn’t advised for logging calls.
Union operators for dicts
Python 3.9 added a union operator
| to dicts (PEP 584), so joining
dicts became a lot easier:
Type hinting generics
This was a small but good addition that saves you an extra import anytime you want to use a
tuple, or a
dict in a type definition (see PEP 585 for the full list of generics that we can now use).
New PEG parser
The LL(1) parser was removed in favor of a PEG parser (PEP 617). A big change for Python, but nothing tangible in the current release. This paved the way for things like the
match expression and better error reporting in Python 3.10.
New Time Zone Database library
PEP 615 added a new module
zoneinfo so that we no longer need to use 3rd party packages (pytz) for dealing with time zone data.
Django switched to using
zoneinfo in version 4.0 so that’s probably going to be a fun upgrade for us 😅
Annual release Cycle
There will now be a new Python release every year, Python 3.10 being the first such release. 🎉
Besides the ones mentioned there have been many more improvements in 3.8 so jump over to the changelogs for 3.8 and 3.9 to read them all.