Fundamentals 7 min read

Explore Python 3.10: bit_count, strict zip, and Read‑Only Dict Views

This article outlines the release timeline for Python 3.8‑3.10 and showcases four major Python 3.10 enhancements—bit_count() for counting set bits, a strict mode for zip(), read‑only mapping proxies for dictionary views, and the removal of deprecated collection ABC aliases—complete with illustrative code snippets.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Explore Python 3.10: bit_count, strict zip, and Read‑Only Dict Views

We are currently in the stable era of Python 3.8, with the latest stable release 3.8.4. Python 3.9 is in beta (first beta 3.9.0b4 released on July 3, 2020) and its first stable version is expected in October 2020. The first beta for Python 3.10 is anticipated in May 2021.

For Python enthusiasts, an exciting period lies ahead, prompting a review of the release schedule for versions 3.8, 3.9, and 3.10.

"My mother always said life is like a box of chocolates. You never know what you're gonna get." – Forrest Gump

Typically, a development cycle includes 4–5 beta releases, after which no new features are added. Beta‑1 for 3.8 was released in June 2019; beta‑1 for 3.9 arrived in May 2020. Although Python 3.10 is just starting, the official site already highlights some of its new features.

This article briefly presents the timeline and previews the main features of the upcoming Python releases, adapting examples from the official Python website.

Highlights of Python 3.10 Features

(1) bit_count() for Counting Set Bits

A new method bit_count() returns the number of ones in the binary representation of an integer, independent of its sign. This is useful in information theory, e.g., computing Hamming distance.

# Positive integer
>>> num = 108
>>> bin(num)
'0b1101100'
>>> num.bit_count()
4
# Negative integer
>>> num = -108
>>> bin(num)
'-0b1101100'
>>> num.bit_count()
4
>>> bin(num).count('1')

(2) Strict Mode for zip()

A new optional keyword argument strict is added to zip(). When strict=True, the iterables must have the same length, otherwise a ValueError is raised. Previously, zip would truncate to the shortest iterable.

Before Python 3.10:

>> list(zip(['A','B','C','D'], ['Apple','Ball','Cat']))
[('A', 'Apple'), ('B', 'Ball'), ('C', 'Cat')]

In Python 3.10:

>> list(zip(['A','B','C','D'], ['Apple','Ball','Cat'], strict=True))
Traceback (most recent call last):
... ValueError: zip() argument 1 is longer than argument 2

(3) Read‑Only Views for Dictionaries

The three dictionary view methods keys(), values(), and items() now return objects that include a mapping attribute, providing a read‑only proxy of the original dictionary.

>> fruits = {'Mangos': 12, 'Figs': 100, 'Guavas': 3, 'Kiwis': 70}
>>> keys = fruits.keys()
>>> values = fruits.values()
>>> list(keys)
['Mangos', 'Figs', 'Guavas', 'Kiwis']
>>> del fruits['Figs']
>>> del fruits['Guavas']
>>> print(list(keys), list(values))
['Mangos', 'Kiwis'] [12, 70]
>>> values.mapping
mappingproxy({'Mangos': 12, 'Figs': 100, 'Guavas': 3, 'Kiwis': 70})
>>> values.mapping['Guavas']
3

(4) Removal of Deprecated Collection ABC Aliases

Aliases for abstract base classes in the collections module will be removed starting with Python 3.10. Importing these ABCs from collections will raise a DeprecationWarning and cease to work in Python 3.9.

>> from collections import ABC_Name
DeprecationWarning: Using or importing the ABCs from 'collections' instead of 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working

Source: http://suo.im/5M6ktn (AI派)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythondictionaryzippython-3.10bit_countread-only proxy
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.