Generator hack to avoid an exception

Kruno Saho kruno.saho at gmail.com
Fri Mar 29 14:27:19 EDT 2013


I understand that 'with ignore(...)' will be coming out in Python 3.4 next year, but in the mean time I have added a yield at the end of a particular coroutine.

Here is the 'fixed' version:
http://pastebin.com/c8kbqc4w

Here is the original version, which I believe is more pythonic:
http://pastebin.com/38Fb7N0Y

The reason being is to have my code outside the generator clean and flat(er). I feel that this little hack, while irritating, is more pythonic than a try/except, which only breaks out of an infinite loop, which was not there to begin with. I could replace the infinite loop with an iteration over a range, but that in itself is doing more work unnecessarily, and is just as hackish. I would not do this if the coroutine controlled any resources, as they would need to be cleaned up. I have not seen this issue addressed in PEP8.

What is the consensus on this issue being more/less pythonic, and how acceptable is this practice?

Thanks.



More information about the Python-list mailing list