r/programminghorror • • Oct 26 '26

Python I love Python

303 Upvotes

81 comments sorted by

View all comments

Show parent comment

20

u/marinuso Oct 26 '26

I've seen it used in production code to memoize functions in this manner:

def fib(n, cache = {0: 1, 1: 1}):
    if not n in cache:
	    cache[n] = fib(n-1) + fib(n-2)
    return cache[n]

Except with much more complicated code rather than this simple example.

2

u/bigorangemachine Oct 26 '26

that's actually kinda smart