classmethod和其他装饰器一起使用?

时间:2016-09-21 08:01:26

标签: python python-decorators

我有一个课程如下:

class Filter(object):
    def __init__(self):
        self._alphabet_digits = (
            "abc|def|ghc"  # alphabet
            "|"
            "123|456|789|000"  # digits
        )

        self._mixed = "123abc|456def"  # digits and alphabet  

    def filter_decorate(func):
        def filter_out(cls, line):
            return bool(re.search(func(cls, line)))
        return filter_out

    @property
    def alphabet_digits(self):
        return self._alphabet_digits

    @property
    def mixed(self):
        return self._mixed

    @classmethod
    @filter_decorate
    def out_alphabet_digits(cls, line):
        return cls.alphabet_digits, line

    @classmethod
    @filter_decorate
    def out_mixed(cls, line):
        return cls.mixed, line

尝试时出现以下错误:

filter = Filter()
filter.out_alphabet_digits("123 aflk l 32")

AttributeError: type object 'Filter' has no attribute 'out_alphabet_digists' 我知道属性是绑定到实例filter但在类方法中我应该使用cls作为第一个参数..然后我将每个cls更改为self我得到类似的错误。我怎样才能一起使用classmethod和一个自己创建的装饰器?

0 个答案:

没有答案