Recursion, memoization and mutable default arguments in Python
问题 "Base" meaning without just using lru_cache. All of these are "fast enough" -- I'm not looking for the fastest algorithm -- but the timings surprised me so I was hoping I could learn something about how Python "works". Simple loop (/tail recursion): def fibonacci(n): a, b = 0, 1 if n in (a, b): return n for _ in range(n - 1): a, b = b, a + b return b Simple memoized: def fibonacci(n, memo={0:0, 1:1}): if len(memo) <= n: memo[n] = fibonacci(n - 1) + fibonacci(n - 2) return memo[n] Using a