gpt4 book ai didi

python - 带有返回语句的生成器

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:08 25 4
gpt4 key购买 nike

<分区>

在我的报道中,我对以下案例(python 3.4)摸不着头脑

def simple_gen_function(str_in, sep=""):
if sep == "":
yield str_in[0]
for c in str_in[1:]:
yield c
else:
return str_in
# yield from str_in

str_in = "je teste "
t = "".join(simple_gen_function(str_in))
p = "".join(simple_gen_function(str_in, "\n"))

print("%r %r" % (t, p))
# 'je teste' ''

在生成器中使用 return,在使用 yield from str_in 时未“达到”返回我得到了预期的结果。

这个问题看起来很简单,但我相信在生成器中使用 return 可以解决这个问题。

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