Software Engineer (SDE)pythonintermediate3 interactive MCQs in app
functools Module: partial, lru_cache & reduce
@lru_cache hashable-args & bounded maxsize@wraps metadata preservation in decoratorspartial argument binding
Core Concept
`functools` supplies higher-order helpers cleanly: `@lru_cache(maxsize=...)` (or `@cache`) memoizes pure functions automatically (turning `O(2^n)` top-down DP into `O(n)`!); `partial(fn, fixed_arg)` freezes configuration parameters cleanly; `@wraps(fn)` inside decorators preserves wrapped function `__name__` and `__doc__`!
Worked Example
decorating `def fib(n): return n if n < 2 else fib(n-1) + fib(n-2)` with `@lru_cache(maxsize=128)` drops exponential calls down to `O(n)` with O(1) cache hits!
Common Pitfall
putting `@lru_cache` on a function receiving unhashable `list`/`dict` arguments raises `TypeError: unhashable type`, or forgetting `maxsize` on unbounded per-user IDs causes memory leaks!
Recommended Readings & Practice
Practice functools Module: partial, lru_cache & reduce with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.