解码python函数,其中返回的内容是迭代器[元组] -
想象一个具有这样的签名的函数:
def read_something (file_name__path: str) -> Iterator[Tuple[Sequence1, Sequence2]]:
如果我正在编写函数以基本上迭代文档并返回元组型对象... iterator [tuple [sequence1,sequence1,sequence2] >该行的一部分表明,由于我使用了打字模块,因此返回的项目(如果正确构造)将是由元组组成的迭代器?还是这意味着我仍然必须迭代文档并将我迭代的每个项目分配到实际的元组中(例如元组(项目,项目)。换句话说,
iterator [tuple [tuple [sequence1,sequence2) ]
神奇地将两个列表类似的东西转换为列表变成元组的迭代器?
Imagine a function with a signature such as this:
def read_something (file_name__path: str) -> Iterator[Tuple[Sequence1, Sequence2]]:
If I am writing the function to basically iterate over a document and return a tuple-type object... Does the Iterator[Tuple[Sequence1, Sequence2]]
portion of the line indicate that because I used the Typing modules, the returned item (if properly constructed) will be an iterator consisting of a tuple? Or does that mean that I still must iterate over the document and assign each item that I iterate over into an actual tuple (such as tuple(item, item). In other words, does that Iterator[Tuple[Sequence1, Sequence2]]
magically transform something like two lists into the iterator with the lists becoming a tuple? Or is that more for the reader of the function to be familiar with the returned object type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
- >
绝对什么都没有。它仅表明创建该功能的人可能计划将返回值设置为Iterator
。另外,当出现弹出提示时,IDE将使用此返回类型。mypy
:您可以看到
bad_iterator
的返回值不是iterator
,并且未转换为迭代器
。但是类型错误出现。编辑:如何在没有mypy的情况下检查类型,
它可以在没有mypy的情况下检查返回的值类型,但是您必须在定义的函数之外进行操作:
但是,它无法在此检查中使用通用物。
isInstance(...,迭代器[int])
会引起错误:->
Do absolutely nothing. It only shows that someone who created the function probably planned to set return value as anIterator
. Also this return type will be used by IDE when popup hint appear.mypy
:As you can see return value for
bad_iterator
is notIterator
and didn't converted intoIterator
. But type error appeared.Edit: how to check types without mypy
It is able to check returned value type without mypy, but you have to do it outside the defined function:
However it is not able to use generics in this check.
isinstance(..., Iterator[int])
will raise error: