Memoize Decorator in Python
✓ Published0🌍 Public
Aabehmiel
Last edited Jul 13, 2010
Created on Jul 29, 2017
This example demonstrates a memoize decorator in Python that caches function results to optimize repeated calls. The code defines a `memoize` function using a dictionary as a cache, and a `wrapper` function that checks for cached results before computing new ones, handling missing keys via exception. It applies the decorator to a recursive Fibonacci function (`fib`), showing how the cache reduces redundant recursive computations. The implementation relies on Python’s built-in exception handling and dictionary API, with no external libraries.
AI-generated description