用另一个装饰器包裹python装饰器
在我的Django代码库中,我都有一个通用的装饰器呼叫:
@override_settings(
CACHES={
**settings.CACHES,
"default": generate_cache("default", dummy=False),
"throttling": generate_cache("throttling", dummy=False),
}
)
def test_something():
...
装饰器代码太冗长了。我想将此代码包装到一个名为@use_real_cache
的新装饰器中,因此测试功能看起来更干净:
@use_real_cache
def test_something():
...
如何用另一个装饰器将装饰器包裹起来?
I have a common decorator call throughout my Django codebase:
@override_settings(
CACHES={
**settings.CACHES,
"default": generate_cache("default", dummy=False),
"throttling": generate_cache("throttling", dummy=False),
}
)
def test_something():
...
The decorator code is too verbose. I'd love to wrap this code into a new decorator called @use_real_cache
so the test function looks much cleaner:
@use_real_cache
def test_something():
...
How can I wrap a decorator with another decorator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将其分配给一个值:
这本质上是问题的第一个代码示例中发生的情况,只是您没有将其分配给(显式)变量。
Just assign it to a value:
This is essentially what happens in the first code sample of the question, except that you do not assign it to an (explicit) variable.