Python 3.9 is currently in the 4th 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
Before Python 3.9, the standard ways to merge to dicts were:
- The
.update
method:a.update(b)
- The spread operator (hello JS):
{**a, **b}
Well, we are getting another one. The |
union operator does exactly what it is supposed to do: a union 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 are 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, most likely you will not 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!