python 装饰器类型错误缺少 1 个必需的位置参数
python decorator TypeError missing 1 required positional argument
我正在尝试编写一个装饰器来重复错误函数 N 次,中间的休眠时间越来越长。这是我目前的尝试:
def exponential_backoff(seconds=10, attempts=10):
def our_decorator(func):
def function_wrapper(*args, **kwargs):
for s in range(0, seconds*attempts, attempts):
sleep(s)
try:
return func(*args, **kwargs)
except Exception as e:
print(e)
return function_wrapper
return our_decorator
@exponential_backoff
def test():
for a in range(100):
if a - random.randint(0,1) == 0:
print('success count: {}'.format(a))
pass
else:
print('error count {}'.format(a))
'a' + 1
test()
我一直收到错误消息:
TypeError: our_decorator() missing 1 required positional argument: 'func'
您应该使用:
@exponential_backoff()
def test():
...
整体装饰器没有设计参数可选,所以使用时必须提供()
。
如果想要有关如何使装饰器允许参数列表可选的示例,请参阅:
您也可以考虑使用 wrapt 包来使您的装饰器更简单、更健壮。
要么你寻求@Graham Dumpleton 提供的解决方案,要么你可以像这样修改你的装饰器:
from functools import wraps, partial
def exponential_backoff(func=None, seconds=10, attempts=10):
if func is None:
return partial(exponential_backoff, seconds=seconds, attempts=attempts)
@wraps(func)
def function_wrapper(*args, **kwargs):
for s in range(0, seconds*attempts, attempts):
sleep(s)
try:
return func(*args, **kwargs)
except Exception as e:
print(e)
return function_wrapper
@exponential_backoff
def test():
for a in range(100):
if a - random.randint(0,1) == 0:
print('success count: {}'.format(a))
pass
else:
print('error count {}'.format(a))
'a' + 1
test()
编辑
我的回答 是 不完全正确,请参阅@GrahamDumpleton 的回答,其中显示了如何使我的解决方案尝试可行(即 this link)。现在修复它,谢谢@GrahamDumpleton!
了解什么是装饰器:
@exponential_backoff
def test():
pass
等于:
def test():
pass
test = exponential_backoff(test)
在这种情况下,test
是 def our_decorator(func):
。这就是为什么你在调用 test()
.
时得到 TypeError
更进一步:
@exponential_backoff()
def test():
pass
等于:
def test():
pass
test = exponential_backoff()(test)
在这种情况下,现在 test
就是您所需要的。
此外,functools.wraps
可帮助您将原始函数的所有属性复制到装饰函数中。例如函数名或文档字符串:
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>
我正在尝试编写一个装饰器来重复错误函数 N 次,中间的休眠时间越来越长。这是我目前的尝试:
def exponential_backoff(seconds=10, attempts=10):
def our_decorator(func):
def function_wrapper(*args, **kwargs):
for s in range(0, seconds*attempts, attempts):
sleep(s)
try:
return func(*args, **kwargs)
except Exception as e:
print(e)
return function_wrapper
return our_decorator
@exponential_backoff
def test():
for a in range(100):
if a - random.randint(0,1) == 0:
print('success count: {}'.format(a))
pass
else:
print('error count {}'.format(a))
'a' + 1
test()
我一直收到错误消息:
TypeError: our_decorator() missing 1 required positional argument: 'func'
您应该使用:
@exponential_backoff()
def test():
...
整体装饰器没有设计参数可选,所以使用时必须提供()
。
如果想要有关如何使装饰器允许参数列表可选的示例,请参阅:
您也可以考虑使用 wrapt 包来使您的装饰器更简单、更健壮。
要么你寻求@Graham Dumpleton 提供的解决方案,要么你可以像这样修改你的装饰器:
from functools import wraps, partial
def exponential_backoff(func=None, seconds=10, attempts=10):
if func is None:
return partial(exponential_backoff, seconds=seconds, attempts=attempts)
@wraps(func)
def function_wrapper(*args, **kwargs):
for s in range(0, seconds*attempts, attempts):
sleep(s)
try:
return func(*args, **kwargs)
except Exception as e:
print(e)
return function_wrapper
@exponential_backoff
def test():
for a in range(100):
if a - random.randint(0,1) == 0:
print('success count: {}'.format(a))
pass
else:
print('error count {}'.format(a))
'a' + 1
test()
编辑 我的回答 是 不完全正确,请参阅@GrahamDumpleton 的回答,其中显示了如何使我的解决方案尝试可行(即 this link)。现在修复它,谢谢@GrahamDumpleton!
了解什么是装饰器:
@exponential_backoff
def test():
pass
等于:
def test():
pass
test = exponential_backoff(test)
在这种情况下,test
是 def our_decorator(func):
。这就是为什么你在调用 test()
.
TypeError
更进一步:
@exponential_backoff()
def test():
pass
等于:
def test():
pass
test = exponential_backoff()(test)
在这种情况下,现在 test
就是您所需要的。
此外,functools.wraps
可帮助您将原始函数的所有属性复制到装饰函数中。例如函数名或文档字符串:
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>