В этом фрагменте кода, почему использование for приводит к отсутствию StopIteration или циклу for перехватывает все исключения и затем молча завершается? В каком случае, почему у нас есть посторонний return? Или raise StopIteration вызвано: 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)
Предполагая, что StopIteration вызывается: return None. Когда генерируется GeneratorExit?
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)
Если я вручную сделаю:
c = countdown(10)
c.close() #generates GeneratorExit??
В каком случае, почему я не вижу следа?