Generator hack to avoid an exception

Discussion in 'Python' started by Kruno Saho, Mar 29, 2013.

  1. Kruno Saho

    Kruno Saho Guest

    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.
    Kruno Saho, Mar 29, 2013
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maurer
    Replies:
    3
    Views:
    4,839
    Peter
    Apr 19, 2006
  2. Alexander Malkis
    Replies:
    8
    Views:
    521
    Alexander Malkis
    Apr 14, 2004
  3. TheDustbustr
    Replies:
    1
    Views:
    450
    Sami Hangaslammi
    Jul 25, 2003
  4. Roger23
    Replies:
    2
    Views:
    1,000
    Roger23
    Oct 12, 2006
  5. Replies:
    0
    Views:
    96
Loading...

Share This Page