Cool New Features in Python 3.9 (Overview)
Python 3.9 is here! Volunteers from all over the world have been working on improvements to Python for the past year. While beta versions have been available for some time, the first official version of Python 3.9 was released on October 5, 2020.
Every release of Python includes new, improved, and deprecated features, and Python 3.9 is no different. The documentation gives a complete list of the changes. Below, you’ll take an in-depth look at the coolest features that the latest version of Python brings to the table.
In this course, you’ll learn about:
- Accessing and calculating with time zones
- Merging and updating dictionaries effectively
- Using decorators based on expressions
- Combining type hints and other annotations
00:00
Welcome to Cool New Features In Python 3.9. My name is Chris and I will be your guide. Python is always growing and changing, and this course will introduce you to the new features that were added in Python 3.9. I’ll be taking you through the new time zone library, the new dictionary operators pipe and pipe equals, the expansion to decorator syntax, the introduction of annotated type hints, underlining changes in the parser, new tools for removing string prefixes and suffixes, changes to how type hints work for lists and dictionaries, the graphlib
library, changes to GCD and the addition of LCM to the math
module,
00:44 and new HTTP status codes.
00:48
The datetime
library supports timestamps both with and without time zones. By default, it uses no time zone, and this is called a naive datetime. Using the tz
parameter, you can add a time zone to the datetime stamp, but up until Python 3.9, the only time zone that was provided was that of UTC.
01:09
If you wanted more time zone support than this, you had to code it yourself. Python 3.9 adds the zoneinfo
library. You no longer have to go to a third party or write the code yourself for proper time zone support.
01:24 There are two new operators for merging and updating dictionaries: pipe and pipe equals. Of course, you could merge and update dictionaries before, but these operators would make your code look more succinct. Previously to Python 3.9, the syntax of decorators limited your ability to use certain kinds of structures like dictionaries and arrays in conjunction with a decorator. If you wanted to use dictionaries or arrays, you had to do this in two steps.
01:50
The new syntax of decorators allows you to do this directly. The new form allows anything that results in a callable to be used as a decorator. The typing
library has now had an Annotated
class added, allowing you to store meta information about your type hints. Using this class, you can put even more information on your type hints—things like the units, as well as the variable type. As a first step towards future features, Python’s underlying parser has been upgraded. For now, this is fully compatible with the existing parser, but in the future, this will enable syntax that wasn’t possible before.
02:30
The str
(string) library has two new methods, .removeprefix()
and .removesuffix()
, for removing the prefix and suffix off of a string. And the requirement to use the special types capital-L List
and capital-D Dict
for type hinting generics is no longer necessary.
02:48
A new library called graphlib
has been introduced that does topological sorting, and the math
library has a couple changes. The gcd()
method now supports more than two arguments, and an lcm()
method has been added. Finally, some new HTTP status codes have been added to the HTTPStatus
class.
03:10
Next up, I’ll dive into the new time zone features and the zoneinfo
module.
Become a Member to join the conversation.
i on Nov. 13, 2023
One nitpick: slide 14 has “effecting” which should be “affecting”.