BeginnerCoder

Lesson 20 — lru_cache

Automatic memoization

The @lru_cache decorator caches a function's results for you. fib.cache_info() shows how many calls were answered from the cache (hits).

Why it matters: one line turns an expensive function fast and clean.

Want it another way?
Task

Add @lru_cache to fib, then print fib(30) and the number of cache hits.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Python Intermediate

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.