小编And*_*eas的帖子

在 Python 类型中声明元组的长度

我想知道是否提交给 Tuple[float, ...] 即使我知道元组的长度。

我有一个 Point 和一个 Rect 类,以及 Rect 类中的一个属性aspoints,该属性将作为两个元组返回左上角和右下角的元组。

迭代器有 type Iterator[float],我知道它会给我两个浮点数。我希望属性的返回值是, Tuple[Tuple[float, float], Tuple[float, float]] 因为我知道迭代器将为每个点提供两个浮点数。

我应该提交,并只说它会返回一个Tuple[Tuple[float, ...], Tuple[float, ...]],在它们的长度的文档中留下评论,还是有更好的解决方案?

这是代码。

from dataclasses import dataclass
from typing import Iterator, Tuple

@dataclass
class Point:
    x: float
    y: float

    def __iter__(self) -> Iterator[float]:
        return iter((self.x, self.y))

@dataclass
class Rect:
    x: float
    y: float
    width: float
    height: float

    @property
    def tl(self) -> Point:
        return Point(self.x, self.y)

    @property
    def br(self) -> Point:
        return Point(self.x + self.width, …
Run Code Online (Sandbox Code Playgroud)

python static-typing python-3.x mypy

5
推荐指数
1
解决办法
1908
查看次数

标签 统计

mypy ×1

python ×1

python-3.x ×1

static-typing ×1