MITB Banner

Python 3.9 Released; Check The New Features

Share

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.

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.

PS: The story was written using a keyboard.
Picture of Ram Sagar

Ram Sagar

I have a master's degree in Robotics and I write about machine learning advancements.
Related Posts

Download our Mobile App

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox
Recent Stories

Featured

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

AIM Conference Calendar

Immerse yourself in AI and business conferences tailored to your role, designed to elevate your performance and empower you to accomplish your organization’s vital objectives. Revel in intimate events that encapsulate the heart and soul of the AI Industry.

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed