Python 3.9 : things you don’t know

Python has been meeting the needs of the community and will become the most used language in the future. The next version of Python brings faster process release, performance improvements, simple new string functions, dictionary union operators, and more compatible and stable internal APIs.

Python 3.9 : things you don’t know
Python has been meeting the needs of the community and will become the most used language in the future.
The next version of Python brings faster process release, performance improvements, simple new string functions, dictionary union operators, and more compatible and stable internal APIs.

The article will cover:

  • Dictionary union and iterable update
  • String method
  • Type hint
  • New mathematical functions
  • New parser
  • Addresses in the IPv6 range
  • New module: area information
  • Other language changes

Dictionary union and iterable update

Dictionary union

One of my favorite new features is fluent syntax.
Python 3.9 dict class. If there are two dictionaries a and b, you can now use these operators to merge and update.
We have the merge operator |:

There is also the update operator | =, which will update the original dictionary:
a = {1: 'a', 2: 'b', 3: 'c'}b = {4: 'd', 5: 'e'}a |= bprint(a){1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
If our dictionary shares a common key, then the key-value pairs in the second dictionary will be used:
a = {1: 'a', 2: 'b', 3: 'c', 6: 'in both'}b = {4: 'd', 5: 'e', 6: 'but different'}print(a | b){1: 'a', 2: 'b', 3: 'c', 6: 'but different', 4: 'd', 5: 'e'}

Use Iterables for dictionary updates

Another great performance of the | = operator is the ability to use iterable objects (such as lists or generators) to update the dictionary with new key-value pairs:
a = {'a': 'one', 'b': 'two'}
b = ((i, i**2) for i in range(3))a |= bprint(a){'a': 'one', 'b': 'two', 0: 0, 1: 1, 2: 4}
If we try to use the standard union operator |, a type error will be prompted because it only allows unions between dict types.

String method

removeprefix() and removesuffix()

str.removeprefix (substring: string) string method: if str starts with it, it will return a new string with modified prefix, otherwise it will return the original string.
str.removesuffix (substring: string) string method: if str ends with it, it returns a new string with a modified suffix, otherwise it returns the original string.

The operations performed by these two functions will use string [len(prefix):] as the prefix and string [:-len(suffix)] as the suffix.
These are very simple operations, and therefore very simple functions are implemented. However, considering that these operations may be performed frequently, it is best to have a built-in function to complete this operation.

Type hint

Python is dynamically typed, dynamically assigning data types to variables means that we don't need to specify data types in the code.
But sometimes it can cause confusion!
For static allocation of data types, type hints are usually used. This was introduced in Python 3.5. Starting from 3.5, we can specify the type, but this is more troublesome.
This update really changes that, and you can now use the built-in collection types (List and Dict) as generic types.
Previously, the uppercase types List and Dict had to be called by typing.
ef greet_all(names: list[str]) -> None: for name in names: print("Hello", name)
Now, no need to call List from typing.List

New mathematical functions

The math module adds and improves many auxiliary functions, starting from the improvement of existing functions.
import math#Greatest common divisormath.gcd(80, 64, 152)#8
The gcd function used to calculate the greatest common factor can only be applied to 2 numbers, forcing programmers to perform operations similar to math.gcd(80, math.gcd(64,152)) when processing more numbers. Since Python 3.9, we can apply it to the value of any number.
The first new addition to the math module is the math.lcm function:
#Least common multiple
math.lcm(4, 8, 5) 
math.lcm calculates the least common multiple of its parameters. Like GCD, it allows a variable number of parameters.

New parser

This part is more of a change out of sight, but it may become one of the most significant changes in the future development of Python.
Python 3.9 uses a new PEG-based parser. Previously, Python used LL(1). PEG is more flexible than LL(1) when building new features of the language. According to the official documentation, this flexibility will be reflected in Python 3.10 and later.
The ast module uses the new parser and generates the same AST as the old parser.

IPv6 range address

Another Python 3.9 is a change that can specify the range of IPv6 addresses. The IPv6 range is used to specify which part of the Internet the corresponding IP address is valid.
The range can be specified at the end of the IP address using the% symbol—for example: 3FFE:0:0:1:200:F8FF:FE75:50DF%2—so the IP address is in range 2 , which is the link local address.
Therefore, if you need to use Python to handle IPv6 addresses, you can now handle it like this:
from ipaddress import IPv6Address 
addr = IPv6Address('ff02::fa51%1') 
#"1" - interface-local IP address
Note that when using basic Python operators to compare, two addresses with different ranges are not the same.

New module

Area information

The zoneinfo module introduces the support of the IANA time zone database into the standard library. It adds zoneinfo.ZoneInfo, which is a specific datetime.tzinfo implementation supported by system time zone data.

Other changes

__import__() now adds ImportError to replace ValueError, which usually occurs when the relative import exceeds its top-level package.
"".Replace("", s, n) now returns s instead of an empty string for all non-zero n. Now it is consistent with "".replace("", s).

By default, Python becomes faster

Each revision of Python has performance improvements compared to previous versions. Python 3.9 has two major improvements that can improve performance without requiring any changes to existing code.
The first improvement involves more use of the vector calling protocol, which calls many common functions by minimizing or eliminating temporary objects. Python 3.9 introduced several new built-in functions, including range, tuple, set, frozenset, list, dict-using vectorcall can speed up execution.

Python switches to annual release cycle

So far, Python has been developed and released at a pace of 18 months. PEP 602 proposes that the Python development team adopt an annual release cycle, and the proposal has been accepted.


With each new version released, Python has become faster and more powerful, and it has become easier to manipulate common data types.
Probably not all of these changes are related to your daily programming, but I think at least you have to realize that this is a good thing, because they may come in handy at some point.

What's Your Reaction?