Interesting Features in Python 3.9

Here’s some features I found interesting in the release of Python 3.9 last month. You can see me talk about each of these in my latest video on youtube.

Full Release Notes

Merging and Updating Dictionaries

PEP 584 - Dictionaries now have new union operators to work with them.

Python 3.8:

x = {'key': 'original_value'}
y = {'key': 'updated_value'}

print({**x, **y}) # Python 3.8 unpacking, kind of confusing

Python 3.9:

x = {'key': 'original_value'}
y = {'key': 'updated_value'}

print(x | y)      # PEP 584 - Union

x |= y            # PEP 584 - In-Place Union

print(x)

Removing Prefixes/Suffixes

PEP 616 - Prefixes and suffixes may now be easily removed using .removeprefix() and .removesuffix()

Not to be confused with lstrip() and rstrip() which strip collections of characters.

Python 3.8:

"Dr Ian Wootten".rstrip("Wootten")
"Dr Ian Wootten".rstrip("Woten")

Python 3.9:

"Dr Ian Wootten".removeprefix("Dr ")
"Dr Ian Wootten".removeprefix("Dr ").removesuffix(" Wootten")

Type Hints for Lists and Dictionaries in Standard Collections

PEP 585 - It is no longer neccessary to import from the typing module to use type hints for standard collections like List, Tuple or ChainMap.

Python 3.8

from typing import List

squares: List[int] = [1, 4, 9]

Python 3.9

squares: list[int] = [1, 4, 9]

Flexible Annotation Hints

PEP 593 - Allows other uses for annotations than just the type itself, so we can include additional metadata, such as comments in the example below.

Python 3.8:

def square_number(number: int) -> int:
	return number * number

Python 3.9:

from typing import Annotated

def square_number(number: Annotated[int, "The number to square"]) -> Annotated[int, "the squared number"]:
	return number * number

Annotations can be inspected by using the __annotations__ attribute:

square_number.__annotations__

Typing also now has a get_type_hints() method to provide details on the types used.

from typing import get_type_hints

get_type_hints(square_number)

Time Zone Support with ZoneInfo

PEP 615 - Python has only ever had time zone support via 3rd party modules like dateutil/pytz. Only UTC is in Python 3.8 and lower out of the box.

zoneinfo in Python 3.9 allows you to retrieve timezone information providing you have an Internet Assigned Numbers Authority (IANA) timezone database on your OS. Some OS’s don’t (i.e Windows) but you can install tzdata from PyPI as an alternative.

This example shows when the datetime in London has just become Christmas Day 2020, it’s still 7pm Christmas Eve in New York.

Python 3.8:

from datetime import datetime
from pytz import timezone

london_tz = timezone("Europe/London")
new_york_tz = timezone("America/New_York")

christmas_day = datetime(2020, 12, 25, 0, 0, tzinfo=london_tz)
christmas_day.astimezone(new_york_tz)

Python 3.9:

from datetime import datetime
from zoneinfo import ZoneInfo

london_tz = ZoneInfo("Europe/London")
new_york_tz = ZoneInfo("America/New_York")

christmas_day = datetime(2020, 12, 25, 0, 0, tzinfo=london_tz)
christmas_day.astimezone(new_york_tz)

If you liked this post, follow me on twitter or join my newsletter below. You'll get updates on new content here and around the web on topics like Python, automation and the cloud once a week.

comments powered by Disqus