Here’s what to expect from Python 3.9
New parser based on PEG
Unlike the older LL(1) parser, the updated version 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 thisrule: 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 whenAdoesn’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.
Now, if you’re already familiar with previous versions of Python then you might know that Python 3.2 introduced a classdatetime.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’re 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 thezoneinfomodule. 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 been 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 used1.update(d2)then it also modifiesd1. So, to fix it, we have to implement a small “hack” something likee = d1.copy(); e.update(d2).
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 felt 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 likestr.lstrip([chars])andstr.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 forremoveprefix()andremovesuffix()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 thetypingmodule?
For example, you can either usetyping.Listor the built-inlist. So, in Python 3.9, the core development team has decided to add support for generics syntax in thetypingmodule. The syntax can now be used in all standard collections that are available in this module.
The major plus of this feature is now users 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 it follows a linear order to tell which task will run before the other.
Thegraphlibmodule enables us to perform a topological sort or order of a graph. It’s mostly used with hashable nodes.
Modules that are enhanced 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 insidegc,http,imaplib,ipaddress,math,os,pydoc,random,signal,socket,time, andsysmodules.
Deprecated Python features
Around 16 features are deprecated in Python version 3.9. You can get detailed information from theofficial 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 withrandommodule then you probably know it can accept any hashable type as a seed value. This can have unintended consequences because there’s 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 themodeargument to open aGzipFilefilefor writing.
What’s been removed?
A total of 21 features that were deprecated in previous versions of Python have now been completely dropped from the language. You may have a look at the complete list onPython’s website.
Thisarticlewas originally published onLive Code StreambyJuan Cruz Martinez(twitter:@bajcmartinez), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.
Live Code Streamis also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI, and computer science in general.