Screen Link: https://app.dataquest.io/m/414/decorators%3A-advanced/1/introduction
My Code:
def counter(func):
def wrapper(*args, **kwargs):
wrapper.count += 1
# Call the function being decorated and return the result
return func(*args,**kwargs)
wrapper.count = 0
# Return the new decorated function
return wrapper
@counter
def foo():
print('calling foo()')
foo()
foo()
print('foo() was called {} times.'.format(foo.count))
Could you tell me what wrapper.count
is in the wrapper function? What is a concept behind this?
This program counts how many times each function is called.
Thank you.