Intern — Junior
Have you used the functools module in your projects?
sobes.tech AI
Answer from AI
Yes, the functools module in Python is often used for working with higher-order functions and optimization. For example, the @lru_cache decorator from functools allows caching of function call results, which speeds up repeated calculations.
Example of using lru_cache:
from functools import lru_cache
@lru_cache(maxsize=32)
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(10)) # Fast calculation thanks to caching
Also, functools includes tools for partial application of functions (partial), combining comparisons (total_ordering), and other useful utilities.