How would you write this code elegantly?
for i in get_generator():
# ...
# if loop executed then call the function
some_function()
How would you write this code elegantly?
for i in get_generator():
# ...
# if loop executed then call the function
some_function()
If your problem is to detect if there were any items returned by the generator (did the body of the loop execute at least once):
loop_executed = False
for i in get_generator():
loop_executed = True
# ...
if loop_executed:
some_function()
loop_executed = False
for i in get_generator():
loop_executed = True
# ...
# if loop executed, then call the function
if loop_executed:
some_function()
Alternatively, if this get_generator call is not dynamic and does not change its state you can also just do something like
for i in get_generator():
# ...
# if loop executed, then call the function
if get_generator() is not None: # or empty or whatever
some_function()