【问题标题】:How to typehint package class objects in Python?如何在 Python 中键入提示包类对象?
【发布时间】:2017-03-28 15:11:37
【问题描述】:

我正在深入研究 Python 中的类型提示,当我们谈论内置对象的类型提示时,一切都很清楚。但我不明白某些包中对象的类型提示应该是什么。例如,这是我使用 falcon web-framework 的代码:

def on_post(self, req, resp):

当我对变量 req 和 resp 使用类型函数时,我分别得到“类'falcon.request.Request'和“类'falcon.response.Response'”,那么这个变量的类型提示应该是什么?喜欢:

def on_post(self, req: 'falcon.request.Request', resp: 'falcon.response.Response') -> None:

【问题讨论】:

    标签: python type-hinting static-typing


    【解决方案1】:

    使用类本身作为类型:

    from falcon.request import Request
    from falcon.response import Response
    
    # ...
    
    def on_post(self, req: Request, resp: Response) -> None:
        # ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 2019-07-08
      • 2022-06-23
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      • 2021-08-03
      相关资源
      最近更新 更多