Emerging Python Features Intermediate¶
When you'd use this
New syntax and standard-library additions across recent Python versions.
Stay current with newly added language features so you write modern Python and adopt improvements early.
What you'll learn¶
- Structural pattern matching (
match) - Modern union type syntax
- Exception groups and
except* - Built-in TOML reading
- How to track what's coming next
The examples here are verified on Python 3.13 (the version this site is built with). Each feature notes the version it arrived in.
Structural pattern matching (3.10+)¶
Structural pattern matching in Emerging Python Features — what it is and when to use it.
The match statement brings powerful, readable branching — far beyond a simple switch. It can match values, guards, and structure. Runnable:
def http_status(code):
match code:
case 200: return "OK"
case 404: return "Not Found"
case n if n >= 500: return "Server Error" # guard condition
case _: return "Unknown" # wildcard
print(http_status(200)) # OK
print(http_status(503)) # Server Error
print(http_status(302)) # Unknown
Output:
The real power is matching structure — destructuring tuples, lists, and objects in one step:
def describe(point):
match point:
case (0, 0): return "origin"
case (x, 0): return f"x-axis at {x}"
case (0, y): return f"y-axis at {y}"
case (x, y): return f"point {x},{y}"
print(describe((0, 0))) # origin
print(describe((5, 0))) # x-axis at 5
print(describe((3, 4))) # point 3,4
Output:
Notice case (x, 0) both matches the shape (a 2-tuple ending in 0) and binds x — that combination of matching and destructuring is what makes it more than a switch.
Modern union types (3.10+)¶
Write int | str and X | None instead of Union/Optional.
Write union types with | instead of typing.Union, and X | None instead of Optional[X]:
def parse(x: int | str) -> str: # was: Union[int, str]
return str(x)
def find(key: str) -> dict | None: # was: Optional[dict]
...
Cleaner and now the recommended style. See Typing & Type Hints.
Exception groups & except* (3.11+)¶
Raise and selectively handle multiple simultaneous errors.
When multiple things can fail at once (e.g. concurrent tasks), ExceptionGroup bundles several exceptions, and except* handles them by type. Runnable:
try:
raise ExceptionGroup("multiple failures", [
ValueError("bad value"),
TypeError("bad type"),
])
except* ValueError:
print("handled a ValueError")
except* TypeError:
print("handled a TypeError")
Output:
Both handlers fire — except* processes each matching exception in the group, unlike normal except which stops at the first match. This matters for asyncio.TaskGroup, where several tasks may fail simultaneously.
Built-in TOML reading (3.11+)¶
Parse TOML with the stdlib tomllib — no dependency.
tomllib reads TOML (the format of pyproject.toml) with no third-party dependency:
import tomllib
config = tomllib.loads('''
title = "demo"
[owner]
name = "Alice"
''')
print(config["title"]) # demo
print(config["owner"]["name"]) # Alice
Output:
Note: tomllib is read-only by design; to write TOML you still need a third-party library.
Other notable recent additions¶
A roundup of smaller recent language and stdlib improvements.
- f-string improvements (3.12) — f-strings became more flexible (nested quotes, multiline expressions).
Selftype (3.11) — annotate methods that return their own class cleanly.- Faster CPython (3.11+) — significant speed gains, no code change needed (see Runtime Evolution).
- Per-interpreter GIL / free-threading (3.12–3.13, experimental) — the beginning of true parallelism (see Runtime Evolution).
- Improved error messages (3.10+) — Python now points at the exact spot and often suggests the fix.
Staying current¶
Where to track what's new each release.
- What's New docs — every release has an official "What's New in Python 3.x" page; the fastest way to see additions.
- PEPs — accepted proposals show what's landing next (Research → PEP Tracker).
- Try the alpha/beta — install a pre-release in a throwaway environment to experiment before it's stable.
Adopt with your minimum version in mind
A feature is only usable if every environment running your code supports it. match needs 3.10+, tomllib needs 3.11+. Check your project's requires-python before reaching for the newest syntax.
Practice exercises¶
- Rewrite an
if/elifchain that dispatches on a value's type usingmatchwith class patterns. - Use
matchto destructure a dict withcase {"type": "user", "name": name}. - Trigger an
ExceptionGroupfrom two failing operations and handle each withexcept*. - Read your own
pyproject.tomlwithtomlliband print the project name. - Read the "What's New" page for the latest Python release and list two additions you'd use.
💬 Discussion
Have a question about this topic? Found an error? Share your thoughts below.