How to organize loop with long prologue and epilogue that switches depending on task

I have the strong feeling that we are not understanding each other.

My change does not add any extra = as they would already exist anyway, no? Instead of defining a new variable (as you said you do in the middle of a loop) you instead set a field in a Dict or any other structure, then you do not need to make changes to the epilogue! call when new variables appear or old ones stop being used. You can also pass any hooks, this is, functions to be called at specific points of the processing, and they will have easy access to all the data.