gpt4 book ai didi

python - 生成器在 Python 中有什么类型签名?

转载 作者:太空狗 更新时间:2023-10-29 20:55:53 26 4
gpt4 key购买 nike

鉴于新的 Python 3.5 允许使用类型签名进行类型提示,我想使用这个新功能,但我不知道如何使用以下结构完全注释函数:

def yieldMoreIfA(text:str):
if text == "A":
yield text
yield text
return
else:
yield text
return

什么是正确的签名?

最佳答案

有一个Generator[yield_type, send_type, return_type] type :

from typing import Generator

def yieldMoreIfA(text: str) -> Generator[str, None, None]:
if text == "A":
yield text
yield text
return
else:
yield text
return

关于python - 生成器在 Python 中有什么类型签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33918325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com