functools python code example
Example 1: python functions
# first we have to write 'def'
# then our function name followed by ()
# and a ':' abd defining block of code
def multiply(): # naming convention could be same as variable for functions
product = 10.5 * 4
return product
product = multiply()
print(product)
Example 2: python funtion
def nameOfFunction(something):
return something
Example 3: python functools
@cache
def factorial(n):
return n * factorial(n-1) if n else 1
>>> factorial(10) # no previously cached result, makes 11 recursive calls
3628800
>>> factorial(5) # just looks up cached value result
120
>>> factorial(12) # makes two new recursive calls, the other 10 are cached
479001600