Understand what decorator is:
@exponential_backoff
def test():
pass
equals to:
def test():
pass
test = exponential_backoff(test)
In this case, test
is def our_decorator(func):
. That's why you get TypeError
when calling test()
.
So further:
@exponential_backoff()
def test():
pass
equals to:
def test():
pass
test = exponential_backoff()(test)
In this case, now test
is what you need.
Further, functools.wraps
helps you to copy all properties of original function to decorated function. Such as function's name or docstring:
from functools import wraps
def exponential_backoff(func):
# @wraps(func)
def function_wrapper(*args, **kwargs):
pass
return function_wrapper
@exponential_backoff
def test():
pass
print(test) # <function exponential_backoff.<locals>.function_wrapper at 0x7fcc343a4268>
# uncomment `@wraps(func)` line:
print(test) # <function test at 0x7fcc343a4400>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…