通常不需要在Python类成员函数中关心self
变量,但是我正在实现一个装饰器,看起来像
def extractor(depends: List[Text]=None,
provides: List[Text]=None) -> Callable[[ExtractorFunction],
ExtractorFunction]:
def _func_wrapper(func: ExtractorFunction) -> ExtractorFunction:
func.is_extractor = True # The value is not important through
func.depends = depends
func.provides = provides
@functools.wraps(func)
def _wrapper(self, parsed: CCDATA, target: ExtractResult) -> None:
func(self, parsed, target)
return _wrapper
return _func_wrapper
旨在像这样使用:
class Extractor():
@extractor()
def _extractor_for_xxx(self, parsed: CCDATA, target: ExtractResult) -> None:
pass
ExtractResult
的定义很好,但是我不确定如何定义ExtractorFunction
的类型。有什么想法吗?