What to Expect in Python 3.9
Discover what's new on the next release of Python
Python 3.9 is expected to release on Monday, 05 October 2020. Prior to releasing the official version, the developers had planned to release six alpha, five beta preview, and two release candidates.
[Read what’s python good for.]
At the time of writing this article, the first candidate was recently released on 11 August. Now, we are anxiously waiting for the second release candidate which will probably be available from 14 September.
So, you might be wondering what’s new in Python 3.9. Right?
There are some significant changes that will dictate the way Python programs work. Most importantly, in this recent version, you will get a new parser that is based on Parsing Expression Grammar (PEG). Similarly, merge
| and update
|= Union Operators are added to dict
Let’s have a more in-depth look at all the upcoming features and improvements of Python 3.9.
New Parser Based on PEG
Unlike the older LL(1) parser, the new one has some key differences that make it more flexible and future proof. Basically in LL(1), the Python developers had used several “hacks” to avoid its limitations. In turn, it affects the flexibility of adding new language features.
The major difference between PEG and a context-free-grammar based parsers (e.g. LL(1)) is that in PEG the choice operator is ordered.
Let’s suppose we write this
rule: A | B | C.
Now, in the case of LL(1) parser, it will generate constructions to conclude which one from A, B, or C must be expanded. On the other hand, PEG will try to check whether the first alternative (e.g. A) succeeds or not. It will continue to the next alternative only when
A doesn’t succeed. In simple words, PEG will check the alternatives in the order in which they are written.
Support for the IANA Time Zone
In real-world applications, users usually require only three types of time zones.
- The local time zone of the system
- IANA time zones
Now, if are already familiar with previous versions of Python then you might know that Python 3.2 introduced a class
datetime.timezone. Basically, its main purpose was to provide support for UTC.
In true sense, the local time zone is still not available. But, in version 3.0 of Python, the developers changed the semantics of naïve time zones to support “local time” operations.
In Python 3.9, they are going to add support for the IANA time zone database. Most of the time, this database is also referred to as “tz” or the Olson database. So, don’t get confused with these terms.
All of the IANA time zone functionality is packed inside the zoneinfo module. This database is very popular and widely distributed in Unix-like operating systems. But, remember that Windows uses a completely different method for handling the time zones.
Added Union Operators
In previous versions of Python, it’s not very efficient to merge or update two dicts. That’s why the developers are now introducing Union Operators like
| for merging and
|= for updating the dicts.
For example, earlier when we use
d1.update(d2) then it also modifies
d1. So, to fix it, we have to implement a small “hack” something like
e = d1.copy(); e.update(d2).
Actually, here we are creating a new temporary variable to hold the value. But, this solution is not very efficient. That’s the main reason behind adding those new Union Operators.
Introducing removeprefix() and removesuffix()
Have you ever feel the need for some functions that can easily remove prefix or suffix from a given string?
Now, you might say that there are already some functions like
str.rstrip([chars]) that can do this. But, this is where the confusion starts. Actually, these functions work with a set of characters instead of a substring.
So, there is definitely a need for some separate functions that can remove the substring from the beginning or end of the string.
Another reason for providing built-in support for
removesuffix() is that application developers usually write this functionality on their own to enhance their productivity. But, in most cases, they make mistakes while handling empty strings. So, a built-in solution can be very helpful for real-world apps.
Type Hinting Generics In Standard Collections
Did you ever notice the duplicate collection hierarchy in the
For example, you can either use
typing.List or the built-in
list. So, in Python 3.9, the core development team has decided to add support for generics syntax in the
typing module. The syntax can now be used in all standard collections that are available in this module.
The major plus point of this feature is that now user can easily annotate their code. It even helps the instructors to teach Python in a better way.
Added graphlib module
In graphs, a topological order plays an important role to identify the flow of jobs. Meaning that it follows a linear order to tell which task will run before the other.
graphlib module enables us to perform a topological sort or order of a graph. It is mostly used with hashable nodes.
Modules That Are Enhance in Python 3.9
In my opinion, the major effort took place while improving the existing modules. You can evaluate this with the fact that a massive list of 35 modules is updated to optimize the Python programming language.
Some of the most significant changes happened inside
Around 16 features are deprecated in Python version 3.9. You can get detailed information from the official Python 3.9 announcement . Here, I’ll try to give you a brief overview of the most important things that are deprecated.
If you have ever worked with
random module then you probably know that it can accept any hashable type as a seed value. This can have unintended consequences because there is no guarantee whether the hash value is deterministic or not. That’s why the developers decided to only accept None, int, float, str, bytes, and bytearray as the seed value.
Also, from now onwards you must specify the
mode argument to open a
GzipFile file for writing.
A total of 21 features that were deprecated in previous versions of Python are now completely dropped from the language. You may have a look at the complete list on Python’s website .
Thanks for reading!
Join the Free Newsletter
A free, weekly e-mail with the best new articles, courses, and special bonuses.
We won't send you spam. Unsubscribe at any time.