Rodrigo Girão Serrão: 4 upgrades to the Python standard library in 3.15

3 weeks ago 40
Club.noww.in

Learn about 4 of the most exciting updates made to the Python standard library in 3.15.

This article highlights 4 of the most exciting updates made to the Python standard library in 3.15:

  • new object_hook in json.load: together with frozendict this unlocks a very powerful use case
  • new module math.integer: new module that introduces integer arithmetic functions
  • new re.prefixmatch function: clarifies some confusion that typically arises when using re.match
  • new asyncio.TaskGroup.cancel function: ergonomic way of cancelling all the tasks in a task group

15 days of Python 3.15

To celebrate the release of Python 3.15, over the next 15 business days I'll be writing about a new 3.15 feature every day. Explained clearly and with examples so you don't have to sift through the changelog.

Subscribe to receive this free email series:

Enroll for free 🐍🚀

If you want, you can also check the schedule of the upcoming emails.

New object_hook in json.load

The function json.load now has a parameter object_hook, which can be set to a callable. When set, JSON objects that are decoded into dictionaries are passed to object_hook and replaced by their result.

For example, suppose you have the following JSON data in a file data.json:

{ "name": { "first": "Rodrigo", "last": "Girão Serrão" }, "interests": [ "Python", "Rust", "AI" ] }

Define an object hook and use it when loading the data:

import json def object_hook(d): print(f"Inside object_hook with {d = }.") return d with open("data.json", "r") as f: data = json.load(f, object_hook=object_hook)

Running this code produces two lines of output:

Inside object_hook with d = {'first': 'Rodrigo', 'last': 'Girão Serrão'}. Inside object_hook with d = {'name': {'first': 'Rodrigo', 'last': 'Girão...
Read Entire Article