Open this example in the interactive library
Source code
from functools import cache
@cache
def factorial(n):
return 1 if n < 2 else n * factorial(n - 1)
print(factorial(6))Sample output
720

Memoize recursive work. A self-contained example using the Python standard library.
Open this example in the interactive library
from functools import cache
@cache
def factorial(n):
return 1 if n < 2 else n * factorial(n - 1)
print(factorial(6))720