惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
罗磊的独立博客
雷峰网
雷峰网
量子位
V
Visual Studio Blog
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
月光博客
月光博客
Martin Fowler
Martin Fowler
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
腾讯CDC
Engineering at Meta
Engineering at Meta
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
Jina AI
Jina AI
A
About on SuperTechFans

Anže's Blog

The 15-Year-Old iptables Rule That Broke My DNS Fedidevs 9h Outage Postmortem Letting Claude Upgrade My Raspberry Pi Agents Day Lisbon DjangoCon Europe 2026 How to Safely Update Your Dependencies Speeding Up Django Startup Times with Lazy Imports Typing Your Django Project in 2026 Claude Fixes User Bug Jekyll to Hugo Migration Advent of Code 2025 🎄 Django bulk_update Memory Issue Migrating Gunicorn to Granian Disable Network Requests When Running Pytest Disable Runserver Warning in Django 5.2 Autogenerating og:images with Jekyll Power Outages and Gunicorn PID Files UV with Django Go-like Error Handling Makes No Sense in JavaScript or Python Packages Do Not Match the Hashes Pip Error Gotchas with SQLite in Production Fedidevs Dev Update #2 Django SQLite Production Config Django Streaming HTTP Responses Deploying a Django Project to My Raspberry Pi (Video) Thoughts on Code Reviews Django SQLite Benchmark Django, SQLite, and the Database Is Locked Error No Downtime Deployments with Gunicorn SQLite Write-Ahead Logging
New Features in Python 3.8 and 3.9
Anže Pečar · 2022-01-07 · via Anže's Blog
07 Jan 2022

At my job, we have just upgraded Python from 3.7 to 3.9, and I got super excited about all the new features. This is a blog post of highlights from these two releases.

Assignment expressions

This was a bit of a controversial feature during Python 3.8’s development and PEP 572 was even part of the reason Guido resigned as a benevolent dictator.

The new feature is pretty straightforward, here are some examples straight from the PEP:

# Handle a matched regex
if (match := pattern.search(data)) is not None: # Do something with match
    ...

# A loop that can't be trivially rewritten using 2-arg iter()
while chunk := file.read(8192):
    process(chunk)

# Reuse a value that's expensive to compute
[y := f(x), y**2, y**3]

# Share a subexpression between a comprehension filter clause and its output
filtered_data = [y for x in data if (y := f(x)) is not None]

I remember using the assignment expression only once in a personal project so far but I do feel it’s a nifty way to save a line of code here and there.

Positional-only arguments

PEP 570 added positional-only arguments. These arguments have no externally-usable name and therefore cannot be called with kwargs. You would use them when you don’t want to expose the function parameter name so that changing it later won’t break anyone’s code.


def f(a, b, /, c, d, \*, e, f):

The function definition has two positional-only arguments (a, b), two parameters that can be both positional or keyword (c, d) and two keyword only parameters (e, f).


# Valid cals:
f(1, 2, c=3, d=4, e=5, f=6)
f(1, 2, 3, d=4, e=5, f=6)
f(1, 2, 3, 4, e=5, f=6)

# Invalid calls
f(a=1, b=2, c=3, d=4, e=5, f=6)
f(1, 2, 3, 4, 5, 6)

Positional only-arguments were already used in the Python standard library and I like that they became a core feature. They might not be super interesting in our day-to-day, but library authors do appreciate them.

Self-documenting expressions

This is a feature that I use a lot when debugging. Appending the = character to the f-string expression will print out the name of the variable used:


# Valid cals:
>>> user = "anze_pecar"
>>> member_since = date(2012, 1, 26)
>>> f"{user=} {member_since=}"
'user=anze_pecar member_since=datetime.date(2012, 1, 26)'

Very useful for debugging, but remember that using f-strings isn’t advised for logging calls.

Union operators for dicts

Python 3.9 added a union operator | to dicts (PEP 584), so joining dicts became a lot easier:


>>> d = {'spam': 1, 'eggs': 2, 'cheese': 3}
>>> e = {'cheese': 'cheddar', 'aardvark': 'Ethel'}
>>> d | e
{'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
>>> e | d
{'cheese': 3, 'aardvark': 'Ethel', 'spam': 1, 'eggs': 2}

Type hinting generics

This was a small but good addition that saves you an extra import anytime you want to use a list, tuple, or a dict in a type definition (see PEP 585 for the full list of generics that we can now use).


l: list[dict[str, str]] = []

New PEG parser

The LL(1) parser was removed in favor of a PEG parser (PEP 617). A big change for Python, but nothing tangible in the current release. This paved the way for things like the match expression and better error reporting in Python 3.10.

New Time Zone Database library

PEP 615 added a new module zoneinfo so that we no longer need to use 3rd party packages (pytz) for dealing with time zone data.

Django switched to using zoneinfo in version 4.0 so that’s probably going to be a fun upgrade for us 😅

Annual release Cycle

There will now be a new Python release every year, Python 3.10 being the first such release. 🎉

Fin

Besides the ones mentioned there have been many more improvements in 3.8 so jump over to the changelogs for 3.8 and 3.9 to read them all.