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:
@nedbat I've been told that subclassing collections.UserDict would be better, as dict itself may optimize away and miss calling some dunder methods sometimes.
@nedbat you are describing a namespace. Used to build dynamic classes.
Spend time learning how to create dynamic classes
This is where to start
https://docs.python.org/3/library/types.html#types.new_class
properties, method, classmethod, staticmethod all have their own boilerplate wrappers.
@nedbat Ned Batchelder is Python's Chuck Norris.
`Mostly shouldn't` is advice targeted to everyone except, Ned Batchelder
@nedbat this is actually useful, if, say, you want a dict that will interpolate between two user-defined values of the requested key doesn't exist, for example.
@juliensalort Easy: @lru_cache is a good idea, and this is not! :D