Python 3.9 Features and Language Changes

A look back at Python 3.9: parser changes, built-in collection type hints, dictionary merging and standard-library time zone support.

PYTHON 3.9

Overview

Python's flexibility and ecosystem have made it useful for both hobby projects and enterprise applications. At JBS Dev, we have used Python to build applications that handle Internet-scale volume. This article looks back at the features introduced in Python 3.9, released on October 5, 2020, including changes to the parser, type hints, dictionaries, and time zone support.

Following the end of Python 2 support in 2020, Python 3.9 removed compatibility features that had previously emitted `DeprecationWarning` exceptions. This continued the transition toward a simpler Python 3 codebase.

Under the hood, Python 3.9 introduced a parser based on Parsing Expression Grammar (PEG). The parser verifies that Python code has the syntax needed to be translated into machine language. Replacing the previous parser reduced constraints on Python's grammar and made room for later language changes.

Python 3.9 also expanded function decorators to allow any valid Python expression, where previously only function names or calls were permitted. It added type hints using built-in collection types such as `list` and `dict`, removing the need to import their equivalents from the `typing` module.

# Python 3.9 decorators allow any Python expression
@Controller.get_controller('left').buttons['X'].when_pressed
def trigger_event():
pass

# Python 3.8 collection type-hinting
from typing import List
def check_all_active(settings: List[str]) -> bool:
return True

# Python 3.9 using generic types directly
def check_all_active(settings: list[str]) -> bool:
return True

Python 3.9 also introduces a set-like merge operator (`|`) for dictionaries. Previously, the suggested way of merging dictionaries was with dictionary unpacking, which was introduced in Python 3.5. That method was surprisingly unreadable for Python and seldom used. The introduction of the `|` operator for dictionaries should aid readability, always a welcome improvement.

# Python 3.8 used dictionary unpacking to merge dictionaries
merged_dict = {**dict1, **dict2}

# Python 3.9 using the dictionary merge operator
merged_dict = dict1 | dict2

Often recognized for its robust standard library and immense third-party ecosystem, Python 3.9 continues to expand on things that many engineers need to build robust software. The zoneinfo module brings IANA time zone database support to the standard library, something that has long been reliant upon a third-party library, pytz.

These examples describe the Python 3.9 release rather than the current Python version. Read the Python 3.9 release notes for the full historical changes, and consult the current Python documentation when choosing a version for a new project.

KEEP READING

Related Resources

Contact

Ready to Talk About Your Next Build?

Tell us what you're trying to build, modernize, automate, or fix. We'll help you understand the practical path forward.

Call 888.421.1155