I want to write a function to exit a for loop. Typically, we can
for i in range(10):
if i==8:
break
I hope to write a function like,
def interrupt(i):
if i == val:
...
for i in range(10):
interrupt(i)
Edit
I understand the mechanism and why I can't do this now. Just out of curiosity, does python offer something like 'macro expansion', so that I don't actually create a function, but a name to repalce the code block?
interruptthrow an exception of some kind and catch it in your for loop, breaking, or doing whatever you please after excepting it.breakandcontinuedo different things.breakexits the loop;continueskips the rest of the body and gets the next value from the iterator.