Python 3.9 was released on 5th October 2020, and lots of people got very excited about it. With the new yearly release schedule for new versions of Python, is it really worthy of all the excitement, or is Python 3.9 just a small incremental improvement over Python 3.8?
Python 3.9 is Faster Than Python 3.8
It Builds Faster
One of the biggest changes made to this version of Python is one you probably won’t care too much about. Under the covers the Python 3.9 parser has been changed from an LL(1) type (Left-to-right, Leftmost derivation, 1 token look-ahead) to a PEG (Parsing Expression Grammar) type. This might sound quite academic, but looking through PEP 617 -- New PEG Parser for CPython there seem to be some current language features that were a pain for the existing parser to handle, and some hacking had to be done to make it work. Replacing the parser with a PEG version promises to get rid of all of the nastiness and make adding language features easier. Like any other development, easier changes generally lead to more features in the same time, so this sounds like a plus.
The main things you need to know about the new parser are:
- It’s faster
- It uses up to 10% more memory, which isn’t bad at all.
PEP 617 also refers to tuning things to reduce memory and increase speed even more in future.
So, like I said there’s not a huge amount of impact at the moment from this change for most developers. Of course that doesn’t mean it’s not a good talking point to bring up at your next job interview.
It Runs Faster
The new version of Python uses optimizations in the background to reduce the number of temporary objects that get created and passed around. This is always a good thing to do, and is one of several changes that have resulted in code running faster under Python 3.9.
Speed
Of course, if Python 3.9 wasn’t at least a little faster than 3.8, that would be pretty poor. I realize that none of us is chasing every last CPU cycle if we’re developing in Python, if we were we’d either switch to PyPy or just make the leap to a regular compiled language. Despite this, there’s always room for speed increases in Python, and they’re always welcome.
New Functions
Dictionaries
For years, Python has needed an intuitive and simple way to merge multiple dictionaries. Up until Python 3.5, it was assumed you’d just write a function for this everyday function then, as of version 3.5, someone decided that this was a great way to assign the union of dictionaries b
and c
to dictionary a
:
a = { **b, **c }
Thankfully this was only a temporary solution, lasting only 5 short years, and has been replaced with the much more intuitive:
a = b | c
There’s also the compound operator:
a |= b
Which updates a to be the union of the original a and b.
Strings
Two new functions have been added to the string object:
string.removeprefix()
string.removesuffix()
These might not strike you as an overly-important pair of functions, but the requirement for these functions arises in a lot of systems, and now you can do this without worrying about string lengths, indexes, etc. And the associated expensive function calls. You may also be able to get rid of some custom methods from your own code, and that’s never a bad thing.
Type Hints For Collections
When I started learning to program in Python, I was told that checking the types of objects passed in to functions was not Pythonic, and I can sort of see that. I still prefer the type safety and method overloading of typesafe languages like C++ and C#, but that’s not what Python does.
Until, Python 3.5, that is, when to my cynical mind, type safety started to be surreptitiously added to the language, at least to a certain extent. This made the code clearer to read, but if you wanted to specify a parameter or return value as being one of Python’s built-in collections, you had to depend upon the typing library. Python 3.9 removes that dependency, simplifying your code.
What Else Is There?
There are improvements to many of the standard modules, improving speed and security across many areas of the language.
So, Should I Upgrade To Python 3.9?
Yes; you should always keep your version of Python up to date, even though there’s always some work involved with testing, removing obsolete code etc. Not upgrading might seem sensible, even attractive, but then before you know it your version of Python is EOL and you’ve opened up your system to all sorts of security vulnerabilities because there are no more patches. This might get even more urgent
Rather than “Should I upgrade?” you really need to be asking “How urgent is it for me to upgrade?”
Your Current Version
None - I Don't Know Much About Python!
That's easy to fix - just install the latest version you can (3.8 or 3.9) and take a quick look at the Five Minute Introduction: Python.
Python 2.7
This could be a big job, so you should add stories to your backlog to get started. Given you’re running something that is now completely unsupported and has been since January 2020, the quicker you get started the better.
Python 3.5 and older
These versions are currently out of data, but the upgrade to 3.9 shouldn’t be too hard. You should put this upgrade at the top of your backlog or task list.
Python 3.6 and newer
While these versions are not out of date at all, it’s still best to upgrade. Add this upgrade to a sensible place in your backlog or task list.
Are There Any Reasons I Shouldn't Upgrade To Python 3.9?
Yes. There's one that nobody seemed to mention much before the release date: Python 3.9 won't run on Windows 7. Yes, I know Windows 7 is past EOL, and so it's not really a surprise, but maybe a little louder warning would have been nice.
I looked around the web a little, and the only reason I could find for explicitly preventing installation on Windows 7 is in the "Installer News" section of the Python 3.9 release notes:
This is the first version of Python to default to the 64-bit installer on Windows. The installer now also actively disallows installation on Windows 7. Python 3.9 is incompatible with this unsupported version of Windows.
Anyway, it may be worth taking that as a hint that you need to upgrade your Windows 7 to something a bit newer, or maybe use a Virtual Machine to run Linux. Alternatively, why not make your PC dual boot and install Linux separately? After all, if the developers of Python don't want to make Python 3.9 compatible with Windows 7, they won't be the last to give up on it.