def counter(fn):
count = 0
def inner(*args, **kwargs):
nonlocal count
count += 1
print('Function {0} was called {1} times'.format(fn.__name__, count)) # {0} = function name (fn.__name__); (1) = count
return fn(*args, **kwargs)
return inner # return the value 'inner' which is an address
def add(a, b=0):
return a + b #a + b = a + 0 = a
print(id(add)) #address of add in line 10
add = counter(add) # counter(add) return a vlue of inner in line 9, which is an address, add = inner, with a variable count = 0
print(id(add)) # id(add) = id(inner) or it's an address of inner
add(1,1)
add(2,2)
# decorator is used to modify function add. But we don't change the function add, but enhance more things to run
# we add a variable 'count' into function add.
# we don't want to change the functionality of function add, but add additional characteristics into it,