for some readability and searchability, here is the typed out code
def counter(func):
def wrapper(*args, **kwargs):
wrapper.count += 1
return func (*args, **kwargs)
wrapper.count = 0
return wrapper
@counter
def foo():
print('calling foo()')
foo()
foo()
print('foo() was called {} times.'.format(foo.count))
currently learning how decorator works...encounter this bit of code
couldn't understandwrapper.count = 0
and how when using the decorate in the following code, that the wrapper.count = 2
but not reset to 0
does this have anything to do with closure?
I visited other post which contains similar problem. the answer wasn't that clear to me
can anyone explain the logic behind this code?
Thanks!