如何在Python中提示自变量的类型

时间:2018-12-24 23:53:19

标签: python-3.x typing

通常不需要在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的类型。有什么想法吗?

0 个答案:

没有答案