New Features in Python 3.10: Union Types, zip strict mode, parenthesized context managers, explicit type aliases, match‑case, improved error messages and other updates
Python 3.10, released on October 4 2021, introduces concise Union type syntax, a strict mode for zip, parenthesized with‑statements, explicit TypeAlias declarations, structural pattern matching via match‑case, clearer syntax error messages, and several deprecations such as distutils, all illustrated with code examples.
Python 3.10 was officially released on October 4 2021. This article summarizes the most important changes compared with earlier versions.
New Union Type syntax
The new version simplifies Union type usage, replacing the verbose Union[int, str] with the concise int | str .
<code>from typing import Union
a: Union[int, str] = 1
</code>becomes
<code>a: str | int = 1
</code>Both expressions are equivalent:
<code>Union[int, str] == int | str # True
</code>The same simplification applies to other generic types such as List[int | str] , dict[str, int | float] , etc.
zip strict mode
The zip function now accepts an optional strict argument. When strict=True , the two iterables must have the same length; otherwise a ValueError is raised.
<code>names = ["a", "b", "c", "d"]
numbers = [1, 2, 3]
z = zip(names, numbers)
for each in z:
print(each)
# ('a', 1)
# ('b', 2)
# ('c', 3)
</code>With strict=True :
<code>z = zip(names, numbers, strict=True)
# ValueError: zip() argument 2 is shorter than argument 1
</code>Parenthesized context managers
The with statement now allows parentheses, enabling grouping of multiple context managers.
<code>with (CtxManager() as example):
...
with (
CtxManager1(),
CtxManager2()
):
...
with (CtxManager1() as example, CtxManager2()):
...
</code>Example with files:
<code>import pathlib
p = pathlib.Path()
p1 = p / "text1.txt"
p2 = p / "text2.txt"
with (
p1.open(encoding="utf-8") as f1,
p2.open(encoding="utf-8") as f2
):
print(f1.read(), f2.read(), sep="\n")
</code>Explicit type aliases
Using TypeAlias makes type aliases explicit and improves readability.
<code>from typing import TypeAlias
x: TypeAlias = int
def plus_int(a: x, b: x) -> x:
return a + b
</code>match...case statement
Python now supports structural pattern matching, similar to a switch‑case construct.
<code>day = 6
match day:
case 1:
print("Monday")
case 6 | 7:
print("Weekend")
case _:
print("Other")
</code>Pattern matching is useful for command‑line parsing, object matching, and dictionary matching. Example with objects:
<code>class Person: ...
class Student(Person):
def __init__(self, id: int) -> None:
self.id = id
class Teacher(Person):
def __init__(self, name: str) -> None:
self.name = name
a = Student(1)
match a:
case Student(id=2):
print(f"This is a student with id 2")
case Student():
print(f"This is a student, id={a.id}")
case Teacher():
print(f"This is a teacher, name={a.name}")
</code>Example with dictionaries:
<code>d = {"name": "Li Si", "age": 18, "hobby": "reading"}
match d:
case {"name": "Zhang San", **args}:
print("This is Zhang San", args)
case {"name": name, "age": age, "hobby": hobby}:
print(f"My name is {name}, I am {age} years old, and I like {hobby}")
</code>For more advanced usage, see PEP 635 and PEP 636.
Friendlier error messages
Python now provides clearer syntax error messages when parentheses or quotes are not closed.
<code>str = "Unclosed string
File "...\Py310探索.py", line 90
str = "Unclosed string
^
SyntaxError: unterminated string literal (detected at line 90)
</code> <code>arr = [1, 2, 2, 3
File "...\Py310探索.py", line 91
arr = [1, 2, 2, 3
^
SyntaxError: '[' was never closed
</code>Other updates
distutils is deprecated; use setuptools instead.
Python now requires OpenSSL 1.1.1 or newer.
The Py_UNICODE API is removed.
The wstr field of PyUnicodeObject is deprecated and will be removed.
That’s all for now – happy coding!
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.