Ned Batchelder on Nostr: Mostly you shouldn't subclass #Python built-in types. But if you do, dict subclasses ...
Mostly you shouldn't subclass #Python built-in types. But if you do, dict subclasses can define __missing__: it's called when a key is missing. Instead of hiding a dict in a function as a cache, how about hiding a function in a dict!? A Fibonacci dictionary:
Published at
2024-06-06 13:36:11Event JSON
{
"id": "7872c7be837879fee79676de9173cbdcf98b3d47aea170ec158abd911ab79efb",
"pubkey": "87773a22627b843246cea7ea7a0ddc5f2cf08371afc51f525e9dcb5835387ce1",
"created_at": 1717680971,
"kind": 1,
"tags": [
[
"proxy",
"https://hachyderm.io/@nedbat/112569940156009404",
"web"
],
[
"t",
"python"
],
[
"imeta",
"url https://media.hachyderm.io/media_attachments/files/112/569/940/016/847/926/original/4e2808208d8b2683.png",
"m image/png"
],
[
"proxy",
"https://hachyderm.io/users/nedbat/statuses/112569940156009404",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://hachyderm.io/users/nedbat/statuses/112569940156009404",
"pink.momostr"
]
],
"content": "Mostly you shouldn't subclass #Python built-in types. But if you do, dict subclasses can define __missing__: it's called when a key is missing. Instead of hiding a dict in a function as a cache, how about hiding a function in a dict!? A Fibonacci dictionary:\nhttps://media.hachyderm.io/media_attachments/files/112/569/940/016/847/926/original/4e2808208d8b2683.png\n",
"sig": "a4d065ec4d54469ecbfa727d9167accffa8e98ca72485f177b2961b27e858093305e61ee6685b8cb42afaa4963ecf8991bb874dcfdd7217274806ab8c995fd99"
}