DEV Community

Edgar
Edgar

Posted on • Originally published at edgarluque.com

1 1

What's new in Python 3.9

Originally published on my own blog

Python 3.9

Python 3.9 has been released on October 5, 2020.

Add Union Operators To dict (PEP 584)

This allows the union operation to be performed on dicts:

>>> a = {'x': 1, 'y': 2, 'z': 3}
>>> e = {'w': 'hello world'}
>>> a | e
{'x': 1, 'y': 2, 'z': 3, 'w': 'hello world'}
Enter fullscreen mode Exit fullscreen mode

And also:

>>> x = a
>>> x
{'x': 1, 'y': 2, 'z': 3}
>>> x |= e
>>> x
{'x': 1, 'y': 2, 'z': 3, 'w': 'hello world'}
Enter fullscreen mode Exit fullscreen mode

Type Hinting Generics In Standard Collections (PEP 585)

This feature enables type hinting using the standard collections without having to rely on the typings module.

Previously to type hint a list you would do:

from typings import List

def somefunc(a: List[int]):
    pass
Enter fullscreen mode Exit fullscreen mode

Now you can use the standard type:


def somefunc(a: list[int]):
    pass
Enter fullscreen mode Exit fullscreen mode

From this version, importing collections from typings is deprecated, and they will be removed in 5 years.

Flexible function and variable annotations (PEP 593)

This feature adds a new type Annotated which allows us to extend type annotations with metadata.

This allows a type T to be annotated with metadata x like so:

T1 = Annotated[T, x]

# E.g
UnsignedShort = Annotated[int, struct2.ctype('H')]
SignedChar = Annotated[int, struct2.ctype('b')]

# Multiple type annotations are supported
T2 = Annotated[int, ValueRange(3, 10), ctype("char")]
Enter fullscreen mode Exit fullscreen mode

The metadata can then be used for static or runtime analysis with tools such as mypy

This feature allows authors to introduce new data types with graceful degradation,
for example if mypy doesn't know how to parse X Annotation it should just ignore its metadata and use the annotated type.

Relaxing Grammar Restrictions On Decorators (PEP 614)

Python currently requires that all decorators consist of a dotted name, optionally followed by a single call. This PEP proposes removing these limitations and allowing decorators to be any valid expression.

An expression here means "anything that's valid as a test in if, elif, and while blocks".

Basically this:

button_0 = buttons[0]

@button_0.clicked.connect
def spam():
    pass
Enter fullscreen mode Exit fullscreen mode

Can now be:

@buttons[0].clicked.connect
def spam():
    pass
Enter fullscreen mode Exit fullscreen mode

Support for the IANA Time Zone Database in the Standard Library

This feature adds a new module zoneinfo that provides a concrte time zone implementation supporting the IANA time zone database.

You can find more about this module here: zoneinfo

Example:

>>> from zoneinfo import ZoneInfo
>>> from datetime import datetime, timedelta

>>> dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo("America/Los_Angeles"))
>>> print(dt)
2020-10-31 12:00:00-07:00

>>> dt.tzname()
'PDT'
Enter fullscreen mode Exit fullscreen mode

String methods to remove prefixes and suffixes

Adds two new methods, removeprefix() and removesuffix(), to the APIs of Python's various string objects.

Image of Bright Data

Feed Your Models Real-Time Data – Enhance your AI with up-to-the-minute data.

Utilize our live data feeds for dynamic model training, ensuring your AI systems are always ahead.

Access Real-Time Data

Top comments (0)

Billboard image

Imagine monitoring that's actually built for developers

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay