logo logo

What’s New In Python 3.9?

What's New in Python 3.9

At the beginning of the month Python version 3.9 was released and as usual, it came with a bunch of interesting features! 🐍

When a new Python version is released, the release notes are usually divided into topics such as: syntax features, built-in features, new features in the standard library, Interpreter improvements, and new library modules.

The goal of this blog post is to discuss some of the features I found interesting and that could benefit our day-to-day life as developers.

📍 Note: In version 3.9, the Python team has removed backward compatibility with the older version of Python 2.7. In the past, features were developed with some backward compatibility, but from version to version, the features were becoming less and less compatible and there’s even a decrease in compatibility with older features. This can be seen in deprecation warnings of old libraries. If you are not sure whether the features are supported or not, use this guide.

New Features in Python 3.9

Operators for merging and updating dictionaries

Additions to the Syntax are one of the things I love the most in new language versions. With this new feature, you can easily update & merge dictionaries.

If up until the latest version we would use the dict.update function and {**d2,** d1} in order to update & merge the following way:

>> d1 = {'oranges': 2, 'apples': 4}
>> d2 = {'bananas': 3, 'oranges': 1}


Merge
>> d3 = {**d1,**d2}
>> d3
{'oranges': 1, 'apples': 4, 'bananas': 3}

Update
>> d1.update(d2)
>> d1
{'oranges': 1, 'apples': 4, 'bananas': 3}

Now, you can use the operator | (pipe/vertical line) to merge between dicts and return a third dict, or we could use the operator |= in order to merge one dictionary into another dictionary in the following manner:

>> d1 = {'oranges': 2, 'apples': 4}
>> d2 = {'bananas': 3, 'oranges': 1}


Merge
>> d3 = d1|d2
>> d3
{'oranges': 1, 'apples': 4, 'bananas': 3}

Update
>> d1|=d2
>> d1
{'oranges': 1, 'apples': 4, 'bananas': 3}

New functions for the type “string” to remove prefixes and suffixes

Up until today, in order to remove a particular part at the end of a string, we needed to use slicing in the following manner:

>> url = 'automatzia.com'
>> if url.endswith('.com'):
..     url = url[:-4]
>> url
'automatzia'

Starting from this new Python 3.9 version, we can simply use the removeesuffix (or removeprefix) function, as below:

>> url = 'automatzia.com'
>> url = url.removesuffix('.com')
>> url
'automatzia'

There’s no doubt that the code looks much more elegant and easier to read. And we’ve saved up on the ‘if’ function while we’re at it 😉

Built-in type hinting on collections

Anyone else tired of importing typing whenever you want hinting for a list or dict? Well, with Python 3.9 you now have that as a built-in option!

Up until the latest version, it should have looked like the following:

>> import typing
>> def print_fruits(fruits: typing.List[str]) -> None:
..     for fruit in fruits:
..         print(fruit)

But now we can simply do the following:

>> def print_fruits(fruits: list[str]) -> None:
..     for fruit in fruits:
..         print(fruit)

Summary

We discussed in this post three new features of Python 3.9 that I found to be the most interesting, but of course, there are plenty of other additions beyond just these three (especially small improvements in libraries). I wouldn’t say that this version had a sensational change, at least not in a way that end users would experience it. That being said, I do recommend diving deeper into the full changes list.

Go ahead and share in the comments below what is your favorite new addition to Python 3.9? ✨

📍 If you are looking for a single Python package for Android, iOS and Web application testing and reporting – there is an open source SDK (OpenSDK) provided by TestProject. With the OpenSDK you can develop and execute robust Python tests of any kind and get automatic cloud based reports for FREE! The OpenSDK supports both Pytest or Unittest frameworks. All you need is: pip3 install testproject-python-sdk.

For any additional Python articles and tutorials, definitely explore the blog’s Python category! 🕵️‍♂️🐍

Leave a Reply

FacebookLinkedInTwitterEmail