Discover Python 3.8’s Game‑Changing Features: Walrus Operator, Audit Hooks & Beyond
Python 3.8 introduces a suite of powerful language enhancements—including the assignment expression (Walrus Operator), mandatory positional‑only parameters, runtime audit hooks, cross‑process shared memory, importlib.metadata utilities, cached_property, decorator simplifications for lru_cache, an interactive asyncio REPL, f‑string debugging, AsyncMock for asynchronous testing, and iterable unpacking fixes—each illustrated with concise code examples.
Assignment Expressions (Walrus Operator)
PEP 572 adds the assignment expression, also known as the "Walrus Operator" ( :=), allowing assignment inside expressions.
fib = lambda n: (lambda f, t, i, a, b: print(f"fib({i}) = {b}") or t == i or f)(lambda f, t, i, a, b: f, 1, 0, 1)Positional‑Only Parameters
PEP 570 enforces the use of positional‑only parameters, causing a TypeError when keyword arguments are supplied to functions like divmod().
Runtime Audit Hooks
Python 3.8 allows adding audit hooks to monitor events such as network requests.
import sys
def audit_hook(event, args):
if event in ['urllib.Request']:
print(f"Network {event=} {args=}")
sys.addaudithook(audit_hook)
import urllib.request
urllib.request.urlopen('https://httpbin.org/get?a=1')Cross‑Process Shared Memory
Shared memory objects can be accessed across processes using multiprocessing.shared_memory.
from multiprocessing import shared_memory
a = shared_memory.ShareableList([1, 'a', 0.1])
# In another process
b = shared_memory.ShareableList(name=a.shm.name)
print(b) # Output: ShareableList([1, 'a', 0.1])Importlib.metadata for Package Introspection
The new importlib.metadata module can retrieve package version, dependencies, and other metadata.
from importlib.metadata import version, files, requires, distribution
print(version('flask')) # e.g., '1.1.1'
print(requires('requests')) # list of requirements
print(distribution('celery').metadata['License'])Cached Property
The cached_property decorator is now part of the standard library, providing a memoized attribute.
lru_cache Without Parentheses
From Python 3.8, functools.lru_cache can be used directly as a decorator without calling it.
@lru_cache
def add(a, b):
return a + bAsyncio REPL
Python now includes an interactive asyncio REPL for experimenting with asynchronous code.
F‑strings Debug
A new debugging feature for f‑strings (though its practical usefulness is limited).
Async Mock
The unittest.mock module adds AsyncMock for mocking asynchronous functions.
import asyncio
from unittest.mock import AsyncMock
mock = AsyncMock(return_value={'json': 123})
result = asyncio.run(mock())
print(result) # {'json': 123}Iterable Unpacking Fix
Python 3.8 resolves issues with iterable unpacking in comprehensions and other contexts.
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.
