TGViewer
Python Learning Python Learning @python_bds · 5.75K subscribers
Post #1072 877
Decorators Are Not Magic. They’re Callbacks in Disguise

You’ve used @lru_cache to speed up a slow function, and it worked... until your app started eating RAM because the cache never forgot anything.

from functools import lru_cache

@lru_cache
def fib(n):
return fib(n-1) + fib(n-2) # ← Cache grows forever!


👉Here’s what’s really happening:
A decorator is just a function that wraps another function. When you write @lru_cache, Python replaces your fib with a new version that remembers every answer it’s ever given. Cool😄 until n goes from 1 to 100,000.

✅ Fix it like a pro:
from functools import lru_cache

@lru_cache(maxsize=128) # Only keep last 128 results
def fib(n):
if n > 1000:
return manual_calc(n) # Skip cache for huge inputs
return fib(n-1) + fib(n-2)


Now the cache stays small, predictable, and safe.

📌Bonus: Write your own @timerdecorator in 5 lines. no more time.time() spam.
  • 👍 2
More from @python_bds
  1. Oct 10, 2026This Book Explains more than 400 Concepts with codes.
  2. Oct 8, 2026🐛 This Python Bug Looks Random, But It Isn't Look at this: functions = [] for i in range(…
  3. Oct 6, 2026🐍 Python Performance Optimization Python Performance Optimization: Make Your Code Faster…
  4. Oct 4, 2026Python Set Methods ✍️
  5. Oct 3, 2026Post #1310
  6. Oct 1, 2026🐍 Python Beginner Notes
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →