What Is New in Python 3.9

Afsalms
The Startup
3 min readSep 15, 2020

--

Python 3.9rc1 was released on September 14, 2020. This release has some good new features, improvements, etc. In this blog, we are going to discuss 7 of the new features

1. Dictionary merge and update operator:

merge (|): This operator is used for merging two dictionaries and returning a new dictionary as an output. If any key is duplicated then the value of the RHS is updated in the final dictionary.

Sample code:

a = {“a”: “a1”, “b”: “b1”}b = {“c”: “c2”, “d”: “d2”}c = a | bprint(f’{a=}’)print(f’{b=}’)print(f’{c=}’)

Output:

a={‘a’: ‘a1’, ‘b’: ‘b1’}b={‘c’: ‘c2’, ‘d’: ‘d2’}c={‘a’: ‘a1’, ‘b’: ‘b1’, ‘c’: ‘c2’, ‘d’: ‘d2’}

Sample code:

a = {“a”: “a1”, “b”: “b1”}b = {“a”: “a2”, “d”: “d2”}c = a | bprint(f’{a=}’)print(f’{b=}’)print(f’{c=}’)

Output:

a={‘a’: ‘a1’, ‘b’: ‘b1’}b={‘a’: ‘a2’, ‘d’: ‘d2’}c={‘a’: ‘a2’, ‘b’: ‘b1’, ‘d’: ‘d2’}

update(|=): This operator is doing the same operation as the merge operator but here instead of creating a new dictionary LHS is getting updated. In case of key duplication the value of RHS gets updated.

Sample code:

a = {“a”: “a1”, “b”: “b1”}b = {“c”: “c2”, “d”: “d2”}a |= bprint(f’{a=}’)print(f’{b=}’)

Output:

a={‘a’: ‘a1’, ‘b’: ‘b1’, ‘c’: ‘c2’, ‘d’: ‘d2’}b={‘c’: ‘c2’, ‘d’: ‘d2’}

2. removeprefix() and removesuffix() methods

removeprefix(): This method returns a text after removing the given prefix

Sample code:

text = “prefixtext”prefix_removed_text = text.removeprefix(“prefix”)print(prefix_removed_text)

Output:

text

Sample code:

text = “abcabctext”print(text.removeprefix(“abc”))

Output:

abctext

removesuffix(): This method return a text after removing the given suffix

Sample code:

text = “textsuffix”suffix_removed_text = text.removesuffix(“suffix”)print(suffix_removed_text)

Output:

text

Sample code:

text = “textabcabc”print(text.removesuffix(‘abc’))

Output:

textabc

3. New parser

Python 3.9 uses a new parser based on PEG instead of LL(1). The performance of the new parser is roughly comparable with the old parser. But this new parser increases the flexibility of Python to accommodate new features in the future.

In Python 3.10 the old parser is completely removed. In Python 3.9 we can switch to the old parser by setting an environment variable PYTHONOLDPARSER=1

4. New module zoneinfo

Added new module zoneinfo which contains the IANA timezone database to our standard library

Sample code:

from datetime import timedeltadt = datetime(2020, 1, 1, 12, tzinfo=ZoneInfo(‘Asia/Kolkata’))print(dt)print(dt.tzname())

Output:

datetime.datetime(2020, 1, 1, 12, 0, tzinfo=zoneinfo.ZoneInfo(key=’Asia/Kolkata’))‘IST’

5. Added close method for multiprocessing.SimpleQueue

Added a close method for the queue. We can explicitly close the queue, methods like get(), put(), empty() no longer work after this. If we tried to access after close it will raise an OSError exception.

Sample code:

a = SimpleQueue()a.put(“test”)a.put(“test”)a.close()a.put(“test”)

Output:

OSError: handle is closed

6. LCM in math module

Added lcm in math module. It returns the lcm of the operand.

Sample code:

import mathprint(math.lcm(100, 150))print(math.lcm(7, 8, 2, 56))

Output:

300
56

7. Builtin Generic Types

We can now use built-in collection types such as list and dict as generic types instead of importing from the typing module.

Python 3.9:

 def greet_all(names: list[str]) -> None:     for name in names:         print(“Hello”, name)

Before Python 3.9:

from typing import Listdef greet_all(names: List[str]) -> None:    for name in names:        print(“Hello”, name)

There are so many other changes in Python 3.9 . You can refer the changes by clicking here.

--

--