单独指定类型提示中的所有变量
考虑此示例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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论