python raise stopiteration_为什么下一次提出'StopIteration',但是'for'是否正常回报?...

In this piece of code, why does using 'for' result in no 'StopIteration'

or is the 'for' loop trapping all exceptions and then silently exiting?

In which case, why do we have the extraneous 'return'?? Or is the

raise StopIteration caused by: return None?

#!/usr/bin/python3.1

def countdown(n):

print("counting down")

while n >= 9:

yield n

n -= 1

return

for x in countdown(10):

print(x)

c = countdown(10)

next(c)

next(c)

next(c)

Assuming StopIteration is being triggered by: return None.

When is GeneratorExit generated?

def countdown(n):

print("Counting down from %d" % n)

try:

while n > 0:

yield n

n = n - 1

except GeneratorExit:

print("Only made it to %d" % n)

If i manually do a:

c = countdown(10)

c.close() #generates GeneratorExit??

In which case why don't I see a traceback?

解决方案

The for loop listens for StopIteration explicitly.

The purpose of the for statement is to loop over the sequence provided by an iterator and the exception is used to signal that the iterator is now done; for doesn't catch other exceptions raised by the object being iterated over, just that one.

That's because StopIteration is the normal, expected signal to tell whomever is iterating that there is nothing more to be produced.

A generator function is a special kind of iterator; it indeed raises StopIteration when the function is done (i.e. when it returns, so yes, return None raises StopIteration). It is a requirement of iterators; they must raise StopIteration when they are done; in fact, once a StopIteration has been raised, attempting to get another element from them (through next(), or calling the .next() (py 2) or .__next__() (py 3) method on the iterator) must always raise StopIteration again.

GeneratorExit is an exception to communicate in the other direction. You are explicitly closing a generator with a yield expression, and the way Python communicates that closure to the generator is by raising GeneratorExit inside of that function. You explicitly catch that exception inside of countdown, its purpose is to let a generator clean up resources as needed when closing.

A GeneratorExit is not propagated to the caller; see the generator.close() documentation.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值