What’s New in Python 3.10? Explore Type Annotations, Union Operator, and More
Python 3.10’s second alpha introduces major enhancements such as expanded type annotations with delayed evaluation, the new ‘|’ union operator, explicit TypeAlias support, a strict mode for zip, int.bit_count() for population count, and mapping views for dictionaries, all aimed at improving code clarity and safety.
New Features in Python 3.10 Alpha 2
Python 3.10’s second alpha, released early November, adds several language enhancements, especially around type annotations.
Type Annotation Extensions
Python 3.9 already overhauled type hints; 3.10 continues this trend by extending annotation capabilities. Annotations are now evaluated lazily, allowing forward references to be expressed as strings and reducing import overhead. Annotations are stored in __annotations__ and evaluated centrally.
Union Operator
Python 3.10 introduces the | operator as a concise way to express unions in type hints (e.g., int | float), replacing the older typing.Union[int, float] syntax.
def f(x: int | float) -> float:
return x * 3.142
f(1) # pass
f(1.5) # pass
f('str') # linter will show annotation errorExplicit TypeAlias
To avoid confusion when using string literals for forward references, the new TypeAlias from typing_extensions marks a name as a type alias.
from typing_extensions import TypeAlias
MyType: TypeAlias = "ClassName"
def foo() -> MyType:
...
# Or, if ClassName is already defined:
MyType: TypeAlias = ClassNameWhy Type Annotations Matter
Type hints improve code readability and maintainability, allowing developers to understand expected input and output types without reading implementation details. They are optional and do not affect runtime behavior.
Other Core Updates
zip strict mode (PEP 618) : zip(..., strict=True) raises ValueError if iterables have different lengths.
int.bit_count() : Returns the population count (number of set bits) of an integer.
Dictionary view mapping : Views returned by dict.keys(), dict.values(), and dict.items() now expose a mapping attribute that returns the original dictionary.
# zip strict example
x = [1,2,3,4,5,6]
y = [1,2,3,4]
z = zip(x, y, strict=True) # raises ValueError
# int.bit_count example
for x in [0,1,2,3,10,11,12,100,101,102]:
print(f"{x} = {x.bit_count()}")
# dict view mapping example
x = {'hello': 0, 'world': 1}
keys = x.keys()
print(keys) # dict_keys(['hello', 'world'])
print(keys.mapping) # {'hello': 0, 'world': 1}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
