单独指定类型提示中的所有变量

发布于 2025-02-13 12:08:55 字数 495 浏览 1 评论 0原文

考虑此示例Dataclass,其中包含一个变量X,该> x 是5个整数的列表。编写类型提示的正确方法是什么?单独指定所有5 int list 中的值似乎很麻烦?如果我的列表是n elements长而n很大?

@dataclass
class Example:
    x: list[int, int, int, int, int] = [0, 0, 0, 0, 0]

我可以写作:

@dataclass
class Example:
    x: list[int] = [0, 0, 0, 0, 0]

还是有其他方式可以编写要向用户表明此列表总是5个元素长的类型提示?

Consider this example dataclass which contains a variable x which is a list of 5 integers. What is the correct way to write the type hint? Individually specifying all 5 int values in the list seems cumbersome? What if my list was N elements long and N was very big?

@dataclass
class Example:
    x: list[int, int, int, int, int] = [0, 0, 0, 0, 0]

Can I just write:

@dataclass
class Example:
    x: list[int] = [0, 0, 0, 0, 0]

Or is there another way I can write the type hint to indicate to the user that this list is always 5 elements long?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文