6 Cool Things You Can Do With The Functools Module - Pybites
In this article let’s look at the functools Standard Library module and 6 cool things you can do with it (be warned, a lot of decorators are coming your way! 😍) … You can use the @cache decorator (formerly called @lru_cache) as a “simple lightweight unbounded function cache”. The classic example is calculating a Fibonacci series where the intermediate results are cached, speeding up the calculation significantly: On my system this code takes 0.02s to complete. 😎 However if I comment the @cache decorator it takes 28.30s because of all the repeated calculations! 😱 Hence caching is especially useful and crucial for tasks with expensive repeat computations. New to caching? Check out our YouTube video. You can do the same for properties using @cached_property. Using the @total_ordering decorator you can write the __eq__() dunder and one of __lt__(), __le__(), __gt__(), or __ge__(), so only two, and it will provide the other ones automatically for you. Less code, nice automation. As per t
In this article let’s look at the functools Standard Library module and 6 cool things you can do with it (be warned, a lot of decorators are coming your way! 😍) … 1. Cache (“memoize”) things You can use the @cache decorator (formerly called @lru_cache ) as a “simple lightweight unbounded function cache”. The classic example is calculating a Fibonacci series where the intermediate results are cached, speeding up the calculation significantly: from functools import cache @cache def fibonacci(n: int) -> int: if n <= 1: return n return fibonacci(n - 1) + fibona
Explore this link on the map →related reading
- Memoization - Wikipediaen.wikipedia.org
- Python Decorators (With Examples)programiz.com
- A practical introduction to functional programmingmaryrosecook.com
- abseil / Performance Hintsabseil.io
- Functional Programming HOWTO — Python 3.14.6 documentationdocs.python.org
- Programming FAQ — Python 3.14.6 documentationdocs.python.org
- What is Memoization? How and When to Memoize in JavaScript and Reactfreecodecamp.org
- An introduction to functional programmingcodewords.recurse.com
- How to Stand Out in a Python Coding Interview – Real Pythonrealpython.com
- Understanding useMemo and useCallback • Josh W. Comeaujoshwcomeau.com
- styleguide | Style guides for Google-originated open-source projectsgoogle.github.io
- Using Flask and Redis to Optimize Web Application Performance | by Fahad Alsaedi | Mediummedium.com