Generator hack to avoid an exception

K

Kruno Saho

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top