Code Explanation:
Import the Required Function
from functools import lru_cache
This imports lru_cache from Python’s functools module.
lru_cache is used to store (cache) function results, so repeated calls with the same input run faster.
Apply the lru_cache Decorator
@lru_cache(None)
This decorator is applied to the function below it.
None means unlimited cache size (no limit).
After applying this, Python will remember the output of the function for each unique input.
Define the Function
def f(x): return x*2
Defines a function f that returns x * 2.
Example: if x = 5, the function output is 10.
Call the Function First Time
f(5)
This calls the function with value 5.
Since it's the first time, the value is computed and stored in cache.
Result = 10 (but nothing is printed yet, because there is no print())
Call the Function Second Time (Cached Result)
f(5)
Same input again → lru_cache returns the stored result from memory.
The function does not compute again (faster), but again nothing is printed.
Print Final Output
print("Done")
Final Output:
Done
.png)

0 Comments:
Post a Comment