我有一个课程如下:
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和一个自己创建的装饰器?