Re: inline exception handling in python

Discussion in 'Python' started by wheres pythonmonks, Aug 12, 2010.

  1. On Thu, Aug 12, 2010 at 2:42 PM, MRAB <> wrote:
    > wheres pythonmonks wrote:
    >>
    >> Hi!
    >>
    >> I have on a few occasions now wanted to have inline-exception
    >> handling, like the inline if/else operator.
    >>
    >> For example,
    >>
    >> The following might raise ZeroDivisionError:
    >>
    >> f = n / d
    >>
    >> So, I can look before I leap (which is okay):
    >>
    >> f = float("nan") if d == 0 else n/d;
    >>
    >> But, what I'd like to be able to write is:
    >>
    >> f = n / d except float("nan");
    >>
    >> Which I find much more appealing than:
    >>
    >> try:
    >>   f = n / d
    >> except:
    >>   f = float("nan")
    >>
    >> (Obviously, I am thinking about more complicated functions than "n/d"
    >> -- but this works as an example.)
    >>
    >> Thoughts?
    >>

    > Discussed a year ago:
    >
    > [Python-Dev] (try-except) conditional expression similar to (if-else)
    > conditional (PEP 308)
    >
    > http://code.activestate.com/lists/python-dev/90256/
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    http://code.activestate.com/lists/python-dev/90256/

    Nice -- excellent discussion and what I was looking for. I am
    guessing that no implementation materialized.
     
    wheres pythonmonks, Aug 12, 2010
    #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. Abhi
    Replies:
    2
    Views:
    750
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    487
    E. Robert Tisdale
    May 6, 2005
  3. Peter
    Replies:
    34
    Views:
    1,973
    James Kanze
    Oct 17, 2009
  4. wheres pythonmonks

    inline exception handling in python

    wheres pythonmonks, Aug 12, 2010, in forum: Python
    Replies:
    0
    Views:
    203
    wheres pythonmonks
    Aug 12, 2010
  5. VSK
    Replies:
    0
    Views:
    253
Loading...

Share This Page