Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're going to learn about lru_cache, a wonderful decorator in Python. Can someone guess what caching means?
Is it about storing things to use later? Like saving images?
Exactly! Just like images can be cached, lru_cache saves the results of function calls. Why do you think this might be useful?
Because it can save time if you're calling the same function repeatedly!
Right! This is especially handy with recursive functions like calculating Fibonacci numbers. Have you all heard of this sequence?
Yes! Each number is the sum of the two preceding ones!
Correct! Let's look at how we can use lru_cache while calculating Fibonacci numbers.
Does it help with the speed a lot?
It does! Let's explore an example next time. Just remember, lru stands for Least Recently Used, which is how the cache decides what to keep.
Here’s a code snippet where we utilize lru_cache. Can someone read this code?
Sure! It defines a function 'fib' that recursively calculates Fibonacci numbers.
Great! Notice how @lru_cache(maxsize=None) is placed before the function. Can someone explain what happens if we call fib(30)?
It calculates the 30th Fibonacci but uses previously cached results, so it runs faster, right?
Exactly! By reusing results from the cache, we avoid recalculating for the same inputs. What’s the output if you run this?
I think it should be 832040!
Correct! Who wants to try modifying the maxsize?
What happens if we set it to a small number?
Good question! If the cache exceeds that size, the oldest cached entries would be discarded. Let's see that in action next session!
Cache it fast, don’t let it go; lru_cache makes functions flow.
Imagine a wise old man who remembers every visitor. He only forgets the least interesting ones. This is how lru_cache remembers function calls!
R a P: Remember and Perform - think about how lru_cache remembers calls to perform faster.
LRU: Least Recently Used - a way to remember how lru_cache decides which results to keep.
Term
What does lru_cache do?
Click to flip
Term
What does the maxsize parameter in lru_cache control?
Click to flip
The lru_cache is a decorator in Python's functools module that enhances function performance by caching results for reuse.
In this section, we learn about the lru_cache decorator from the functools module which allows caching of function results. By storing results of expensive function calls, subsequent calls with the same parameters will utilize the cached result, improving performance significantly. The maxsize parameter controls the cache size.
The lru_cache decorator from Python's functools module is a powerful tool for optimization, especially in functions that are computationally expensive. The term 'LRU' stands for 'Least Recently Used', which indicates how the cache maintains its size. When the cache reaches its maximum size, the least recently accessed items are purged to make room for new entries. This behavior makes it useful for recursive functions like calculating Fibonacci numbers, where many calls with the same parameters occur.
maxsize to control how many results to store in the cache. Setting maxsize=None allows unlimited storage but may increase memory usage.from functools import lru_cache
@lru_cache(maxsize=None)
def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)
print(fib(30)) # Output: 832040In this example, the fib function computes Fibonacci numbers efficiently using lru_cache, showcasing the decorator's utility in handling recursion and improving performance.