python - 生成器函数的返回类型提示是什么?

标签 python python-2.7 generator yield type-hinting

我正在尝试为生成器函数编写 :rtype: 类型提示。它返回的类型是什么?

例如,假设我有这个产生字符串的函数:

def read_text_file(fn):
    """
    Yields the lines of the text file one by one.
    :param fn: Path of text file to read.
    :type fn: str
    :rtype: ???????????????? <======================= what goes here?
    """
    with open(fn, 'rt') as text_file:
        for line in text_file:
            yield line

返回类型不仅仅是一个字符串,它是某种可迭代的字符串?所以我不能只写:rtype:str。正确的提示是什么?

最佳答案

Generator

Generator[str, None, None]Iterator[str]

关于python - 生成器函数的返回类型提示是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43658999/

相关文章:

python - wxPython 处理 SIGTERM/SIGINT

regex - python 替换字符串函数抛出 asterix 通配符错误

python 3 : How to generate a pseudo-random sequence per object?

Python 生成器预取?

python - 我无法循环遍历 Django 模板中的字典

python - 为什么在函数中分配给全局变量时会出现 "referenced before assignment"错误?

python - 在 Ubuntu 中安装 graph-tool 时配置错误(需要 c++14)

python - google.appengine.api.urlfetch 的截止日期是否限制为 60 秒?

python - 同时运行多个python文件

python - python lambda/fn 可以代表任意调用者产生吗?