First of all, the source indentation is part of python syntax; it delimits the blocks. Please follow the instructions at my signature for your future python posts.

Next, the piece of code is a generator, meaning it needs to preserve state. We can do this with static variables. It will also need set up and clean up calls. You'll to also change the way the code is used. Thus, I'll translate more python than you requested. The code might be made more efficient if you also showed how it's used.