【发布时间】:2015-01-01 00:53:13
【问题描述】:
我从stack_overflow_entry 了解到,在 Python 中,装饰器是按照它们在源代码中出现的顺序应用的。
那么下面的代码 sn-p 应该如何表现呢?
@unittest.skip("Something no longer supported")
@skipIf(not something_feature_enabled, "Requires extra crunchy cookies to run")
def test_this():
....
第一个装饰器(如下所述)要求测试运行器完全跳过test_this()
@unittest.skip("Something no longer supported")
而第二个装饰器要求测试运行器有条件地跳过运行test_this()。
@skipIf(not something_feature_enabled, "Requires extra crunchy cookies to run")
那么这是否意味着test_this 根本不会运行,除非我们将条件跳过装饰器放在首位?
另外,Python 中有没有办法定义装饰器的依赖执行?例如
@skipIf("Something goes wrong")
@skipIf(not something_feature_enabled, "Requires extra crunchy cookies to run")
@log
@send_email
def test_this():
....
这个想法是如果@skipIf("Something goes wrong") 是true,则启用@log 和@send_email 的执行。
抱歉,如果我遗漏了一些非常明显的内容。
【问题讨论】:
标签: python dependencies decorator execution python-decorators