解码python函数,其中返回的内容是迭代器[元组] -

发布于 2025-01-24 16:23:32 字数 424 浏览 0 评论 0原文

想象一个具有这样的签名的函数:

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 技术交流群。

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

发布评论

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

评论(1

吾家有女初长成 2025-01-31 16:23:32
  1. - >绝对什么都没有。它仅表明创建该功能的人可能计划将返回值设置为Iterator。另外,当出现弹出提示时,IDE将使用此返回类型。
  2. 如果要检查正确的键入,则可以使用mypy
from typing import Iterator, Tuple

def correct_iterator() -> Iterator[Tuple[int, str]]:
  for el in [(1, '1'), (2, '2')]:
    yield el

def correct_tuple() -> Tuple[int, str]:
  return (1, '1')

def bad_iterator() -> Iterator[Tuple[int, str]]:
  # that is line 12 ↓
  return (1, '1')
mypy example.py
example.py:12: error: Incompatible return value type (got "Tuple[int, str]", expected "Iterator[Tuple[int, str]]")
example.py:12: note: "tuple" is missing following "Iterator" protocol member:
example.py:12: note:     __next__
Found 1 error in 1 file (checked 1 source file)

您可以看到bad_iterator的返回值不是iterator,并且未转换为迭代器。但是类型错误出现。

编辑:如何在没有mypy的情况下检查类型,

它可以在没有mypy的情况下检查返回的值类型,但是您必须在定义的函数之外进行操作:

from typing import Iterator

def the_iterator():
  for el in [1,2,3]:
    yield el

iterator_output = the_iterator()

assert True == isinstance(iterator_output, Iterator)

但是,它无法在此检查中使用通用物。 isInstance(...,迭代器[int])会引起错误:

TypeError: Subscripted generics cannot be used with class and instance checks
  1. -> Do absolutely nothing. It only shows that someone who created the function probably planned to set return value as an Iterator. Also this return type will be used by IDE when popup hint appear.
  2. If you want to check correct typing you may use mypy:
from typing import Iterator, Tuple

def correct_iterator() -> Iterator[Tuple[int, str]]:
  for el in [(1, '1'), (2, '2')]:
    yield el

def correct_tuple() -> Tuple[int, str]:
  return (1, '1')

def bad_iterator() -> Iterator[Tuple[int, str]]:
  # that is line 12 ↓
  return (1, '1')
mypy example.py
example.py:12: error: Incompatible return value type (got "Tuple[int, str]", expected "Iterator[Tuple[int, str]]")
example.py:12: note: "tuple" is missing following "Iterator" protocol member:
example.py:12: note:     __next__
Found 1 error in 1 file (checked 1 source file)

As you can see return value for bad_iterator is not Iterator and didn't converted into Iterator. 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:

from typing import Iterator

def the_iterator():
  for el in [1,2,3]:
    yield el

iterator_output = the_iterator()

assert True == isinstance(iterator_output, Iterator)

However it is not able to use generics in this check. isinstance(..., Iterator[int]) will raise error:

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