小编Okl*_*mer的帖子

PEP0484 类型提示:注释给定类的参数,而不是实例

让我先用一些例子来解释。
假设有一个 Web API 客户端模块(MyAPIClient),一个将任意响应转换为 Python 对象的映射器类(ObjectMapper),以及一个表示响应对象(User 和 Message)的类。

class User(MyResponse):
    def __init__(self, status: int, id: int, name: str) -> None:
        super().__init__(status)
        self.id = int
        self.name = name

class Message(MyResponse):
    def __init__(self, status: int, id: int, text: str) -> None:
        super().__init__(status)
        self.id = int
        self.text = name

class ObjectMapper(object):
    def __init__(self, mapping_class: ???) -> None:
        self.mapping_class = mapping_class

    def map(self, obj) -> MyResponse:
        return self.mapping_class(**kwargs)

class MyAPIClient(object):
    def __init__(self, ...) -> None:
        pass

    def get_current_user(...) -> User:
        self.request("GET", "/current_user", ObjectMapper(User)) …
Run Code Online (Sandbox Code Playgroud)

python types python-3.x

8
推荐指数
2
解决办法
2698
查看次数

标签 统计

python ×1

python-3.x ×1

types ×1