Python 3.9 promises a plethora of new features to save time and improve the efficiency of your Python scripts.
The second release candidate was released on 20 September 2020, and the official release date for Python 3.9 is currently set for October 2020.
Although the full list of features is extremely extensive, in this blog I have covered what we at Thorgate think are some of 3.9’s most awesome offerings!
Dictionary Merge & Update Operators
It is now possible to merge and update dictionaries using the “|” and “|=” operators, respectively.
The way the python dict
class merged and updated dictionaries previously had numerous disadvantages, including:
- Modifying a
dict
object in-place when usingdict.update
- There is a “straightforward” method of “dictionary unpacking” which few typical Python programmers even know about! (i.e.
{**d1, **d2}
) - Duplicate keys being resolved in opposite order when using
collections.ChainMap
(which itself is quite an unknown class added in 3.3)
These and several other non-optimum factors associated with merging and updating dict
objects have now been obliterated by the introduction of the new operators.
The dict
union operator (|) will merge the left operand with the right one. Both operands must be a dict
object (or instance of a dict
subclass). If both operands ever contain the same key, the key from the right operand wins.
For example:
>>> d = {'spam': 1, 'eggs': 2, 'cheese': 3} >>> e = {'cheese': 'cheddar', 'aardvark': 'Ethel'} >>> d | e {'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'} >>> e | d {'aardvark': 'Ethel', 'spam': 1, 'eggs': 2, 'cheese': 3}
The merge operator operates in place, for example:
>>> d |= e >>> d {'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
New Parser
Python 3.9 will switch to a new grammar parser.
The new parser’s performance is about the same as the previous one’s, but the new parser allows for more flexibility when it comes to adding language features in future.
This guarantees future releases with even better language features and optimisations!
The old parser will be completely removed in Python 3.10.
String methods to remove prefixes and suffixes
Python 3.9 will add two new methods to remove prefixes and suffixes — str.removeprefix()
and str.removesuffix()
.
The rationale for this was that the existing str.lstrip
and str.rstrip
methods have continuously created confusion among python programmers who expect these two methods to remove prefixes and suffixes.
Two new methods have been added to reduce this confusion:
str.removeprefix(prefix, /)
If the string begins with the prefix passed in as an argument, then the prefix will be removed and the new string returned.
Otherwise, the old string will be returned.
For example:
>>> 'Thorgate'.removeprefix('Thor') 'gate' >>> 'ThorgateRules'.removeprefix('Rules') 'ThorgateRules'
str.removesuffix(suffix, /)
str.removesuffix()
works in precisely the same way, but removes the value passed as an argument from the end of the string.
>>> 'Thorgate'.removesuffix('Thor') 'Thorgate' >>> 'ThorgateRules'.removesuffix('Rules') 'Thorgate'
Builtin Generic Types
I can almost hear the python teachers amongst you breathing a sigh of relief for this one!
Generic types are defined as:
“Generic (n.) -- a type that can be parameterized, typically a container. Also known as a parametric type or a generic type. For example: dict
.”
As of python 3.9, it is now possible to use built-in collection types (such as list
and dict
) as generic types instead of importing the corresponding capitalized types (e.g. List
or Dict
) from typing
.
New zoneinfo Module
Python 3.9 introduces a new zoneinfo
module which brings support for the IANA time zone database to the standard library.
The IANA timezone database is in the public domain and widely distributed. It is present by default in many UNIX-like systems. The database is extremely stable.
The implementation of this zoneinfo
module in 3.9 is quite extensive. The module aims to simplify timezone implementations as well as add additional functionality which was not easily achieved in previous versions of Python.
Flexible Function and Variable Annotations
Python 3.9 adds the new typing.Annotated
type which facilitates decorating existing types with context-specific metadata.
This is done using type hints.
For example, a type T
can be annotated with metadata m
using the typehint Annotated[T, m]
.
This metadata can then be used for analysis at runtime.
Libraries will continue to treat type T
as type T
unless it has special logic for metadata m
.
PEP 593 describes the syntax:
“Annotated
is parameterized with a type and an arbitrary list of Python values that represent the annotations.”
For example:
Annotated[int, ValueRange(3, 10), ctype("char")]
Less Restrictive Grammar on Decorators
Python 3.9 reduces grammar restrictions when specifying decorators so that these are now more readable.
The PEP describing this calls for allowing decorators to be any valid expression and no longer demands that decorators consist of a dotted name.
Performance Improvements
The performance improvements of Python 3.9 compared to 3.4 are quite staggering.
Benchmarked on an Intel® Core™ i7-4960HQ processor running the macOS 64-bit builds found at python.org, variable and attribute read-access performance has, in many cases, been cut in half, making Python 3.9 almost twice as fast as its predecessors.
The same is true of write-access operations and stack operations.
New, Annual Release Cycle
Python’s release calendar will change as of version 3.9 so that new, major versions will now be released every October, giving developers prediction on upcoming changes.
Each of the 3.X series will be maintained for five years.
For the first eighteen months, the series will receive bugfix updates and full releases, approximately every other month.
For the following forty-two months, the series will receive security updates and source-only releases.
And much more
Of course, there is much more that is coming in Python 3.9. But the above are our favourites here at Thorgate.
I would love to know what new features in Python 3.9 interest you most? Feel free to write to me at madis@thorgate.eu.
Read related articles:
Is your software development team efficient?