I'm implementing customized loop behavior, where I need things to happen on entering the loop, at every loop start, at every loop end, and on exiting the loop area. So far this is beautifully simple in Python (2.7):

```
def my_for(loop_iterable):
    enter_loop()
    for i in loop_iterable:
        loop_start()
        yield i
        loop_end()
    exit_loop()

for i in my_for([1, 2, 3]):
    print "i: ", i
    if i == 2:
        break

```

The problem I'm having is in getting `loop_end()` and `exit_loop()` to execute after the `break`. I have solved this by defining another function, which the user must put before break:

```
def break_loop():
    loop_end()
    exit_loop()

for i in my_for([1, 2, 3]):
    print "i: ", i
    if i == 2:
        break_loop()
        break

```

But I would really like not to have the user have to remember to add that line. I think if I re-write the generator function as an iterator class, maybe there is a way to still execute code on a `break`?

Incidentally, `continue` works just fine as is!