Я изо всех сил пытаюсь выяснить, как добиться реализации, подобной следующей в python:
def a_generator():
i = 0
while True:
yield i
i += 1
# if [downstream function returns false]:
# break
cleanup()
def cleanup():
# Do some cleanup task
pass
def handle_each_value(i):
if i > 10:
return False
return True
for value in a_generator():
continue_processing = handle_each_value()
# Cause the generator to run `cleanup()` if `continue_processing == False`
Есть ли способ сделать это, с помощью обратных вызовов или в качестве генератора?