Now Reading
Python 3.9 Released; Check The New Features

Python 3.9 Released; Check The New Features

Ram Sagar

It was announced last year that Python will be making significant changes and will be adding new features with the release of their 3.9 version. Back then, it was speculated that the new version would flaunt new features in security, core and builtins, libraries, documentation, and more. 

Yesterday, Python announced new features as part of their 3.9 in beta version((3.9.0b3). Let’s take a look at few highlights:

Merging Gets Better

The current ways to merge two dicts in Python have several disadvantages:



dict.update

d1.update(d2) modifies d1 in-place. e = d1.copy(); e.update(d2) is not an expression and needs a temporary variable.



{**d1, **d2}

Dict unpacking looks ugly and is not easily discoverable. Few people would be able to guess what it means the first time they see it, or think of it as the “obvious way” to merge two dicts.

“…if you were to ask a typical Python user how to combine two dicts into a new one, I doubt many people would think of {**d1, **d2}. I know I myself had forgotten about it when this thread started!”

Guido Van Rossum, Python creator

Now in 3.9, Merge (|) and update (|=) operators have been added to the built-in dict class. Here’s how it works:

>>> 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 augmented assignment version operates in-place:

>>> d |= e

>>> d

{'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}

New String Methods

str.removeprefix(prefix) and str.removesuffix(suffix) have been added to easily remove an unneeded prefix or a suffix from a string. 

Current:
if test_func_name.startswith("test_"):

    print(test_func_name[5:])

else:

    print(test_func_name)

Improved:
print(test_func_name.removeprefix("test_"))

Built-in Gets Generic

Static typing was built incrementally on top of the existing Python runtime and constrained by existing syntax and runtime behavior. This led to the existence of a duplicated collection. For example typing.List and the built-in list.

See Also
Exploratory Data Analysis In Python Vs R

The new release enables support for the generics syntax in all standard collections currently available in the typing module.

def greet_all(names: list[str]) -> None:

    for name in names:

        print("Hello", name)

Brand New Modules

zoneinfo

The zoneinfo module brings support for the IANA time zone database to the standard library. It adds zoneinfo.ZoneInfo, a concrete datetime.tzinfo implementation backed by the system’s time zone data.

>>> from zoneinfo import ZoneInfo

>>> from datetime import datetime, timedelta

graphlib

Using the graphlib.TopologicalSorter class of the graphlib, one can perform topological sorting of graphs

Dropped Items

The new Python version ditched a bunch of methods, functions and modules for good. Here are a few:

  • tostring() and fromstring() have been removed
  •  C function PyImport_Cleanup() has been removed.
  • _dummy_thread and dummy_threading modules have been removed.

Find the complete list of updates here.

Provide your comments below

comments


If you loved this story, do join our Telegram Community.


Also, you can write for us and be one of the 500+ experts who have contributed stories at AIM. Share your nominations here.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top