【问题标题】:what is disabled decorator in python?python中禁用的装饰器是什么?
【发布时间】:2013-12-27 19:00:08
【问题描述】:

在阅读一些 python 代码时,我看到了以下内容:

@disabled
class IterCases(BaseMatchCase):

很难找到'disabled'装饰器的含义。看起来“没有”禁用类本身,因为它在运行时被积极使用。

【问题讨论】:

  • 在顶部查找导入;这不是标准库装饰器,而是第三方库。换句话说,我们将不得不猜测并且对此盲目猜测。
  • 寻找一个名为disabled的函数,它可能来自您导入的模块。

标签: python decorator python-decorators


【解决方案1】:

我找到了您似乎正在查看的exact source code

该装饰器在oftest.testutils module 中定义为:

def disabled(cls):
    """
    Testcase decorator that marks the test as being disabled.
    These tests are not automatically added to the "standard" group or
    their module's group.
    """
    cls._disabled = True
    return cls

所以它所做的只是设置一个_disabled 属性。我找到了一个load_test_modules() function,然后使用此属性跳过任何将_disabled 设置为True 的类。

乍一看,装饰器似乎可用于任何类;实际测试用例类使用的类。

【讨论】:

  • 这可能是个愚蠢的问题,但是这个装饰器函数(禁用)是什么时候调用的呢?当我创建实例时?
  • 在类定义运行时应用;所以在这种情况下导入模块时。
猜你喜欢
  • 2016-11-25
  • 1970-01-01
  • 2010-09-27
  • 2012-01-09
  • 2010-09-17
  • 2020-06-29
  • 1970-01-01
  • 2017-10-11
  • 1970-01-01
相关资源
最近更新 更多