python打字:元组[str] vs tuple [str,...]

发布于 2025-01-23 18:15:54 字数 74 浏览 0 评论 0原文

元组[str,...] vs 元组[str]有什么区别? (Python打字)

What's the difference between tuple[str, ...] vs tuple[str]? (python typing)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2025-01-30 18:15:54

用省略者给出元组类型意味着该元组中的元素数量尚不清楚,但是类型是已知的:

x: tuple[str, ...] = ("hi",)          #VALID
x: tuple[str, ...] = ("hi", "world")  #ALSO VALID

不使用省略号意味着具有特定元素数量的元组,例如:

y: tuple[str] = ("hi", "world")  # Type Warning: Expected type 'Tuple[str]', got 'Tuple[str, str]' instead 

这与其他集合的表示法相反,例如list [str] 表示任何长度的列表,其中包含str的元素。

giving a tuple type with ellipsis means that number of elements in this tuple is not known, but type is known:

x: tuple[str, ...] = ("hi",)          #VALID
x: tuple[str, ...] = ("hi", "world")  #ALSO VALID

not using ellipsis means a tuple with specific number of elements, e.g.:

y: tuple[str] = ("hi", "world")  # Type Warning: Expected type 'Tuple[str]', got 'Tuple[str, str]' instead 

This goes in contrast to notation of other collections, e.g. list[str] means list of any length with elements of type str.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文