Python 中的产量中断
根据这个问题的答案,C#中的yieldbreak
相当于Python中的return
。在正常情况下,return
确实会停止生成器。但是如果你的函数除了 return 之外什么都不做,你会得到一个 None,而不是一个空迭代器,它是由 C# 中的yield break 返回的
def generate_nothing():
return
for i in generate_nothing():
print i
。得到一个 TypeError: 'NoneType' object is not iterable
, 但如果我添加并且在 return
之前从未运行 yield
,则此函数将返回我所期望的结果。
def generate_nothing():
if False: yield None
return
它有效,但看起来很奇怪。你有更好的主意吗?
According to answer to this question, yield break
in C# is equivalent to return
in Python. In the normal case, return
indeed stops a generator. But if your function does nothing but return
, you will get a None
not an empty iterator, which is returned by yield break
in C#
def generate_nothing():
return
for i in generate_nothing():
print i
You will get a TypeError: 'NoneType' object is not iterable
,
but if I add and never run yield
before return
, this function returns what I expect.
def generate_nothing():
if False: yield None
return
It works, but seems weird. Do you have a better idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有趣的是,这两个函数具有相同的字节码。当字节码编译器找到
yield
关键字时,可能有一个标志设置为generator
。The funny part is that both functions have the same bytecode. Probably there's a flag that sets to
generator
when bytecode compiler finds theyield
keyword.处理这个问题的一个好方法是引发 StopIteration ,这是当你的迭代器有没有任何东西可以让出并且调用
next()
。这也将优雅地跳出 for 循环,循环内不执行任何内容。例如,给定一个元组
(0, 1, 2, 3)
我想获得重叠的对((0, 1), (1, 2), (2, 3))
。我可以这样做:现在
pairs
可以安全地处理包含 1 个或更少数字的列表。A good way to handle this is raising StopIteration which is what is raised when your iterator has nothing left to yield and
next()
is called. This will also gracefully break out of a for loop with nothing inside the loop executed.For example, given a tuple
(0, 1, 2, 3)
I want to get overlapping pairs((0, 1), (1, 2), (2, 3))
. I could do it like so:Now
pairs
safely handles lists with 1 number or less.