Actually. What if we just reused 'try'?

    foo = bar() except BazException try 'qux'

This also leads naturally to chaining multiple possible fallbacks:

    foo = bar() except BarException try baz() except BazException try None

On Thu Feb 13 2014 at 10:32:16 AM, David Mertz <mertz@gnosis.cx> wrote:
On Thu, Feb 13, 2014 at 3:25 AM, spir <denis.spir@gmail.com> wrote:
Of course, it could be done as a function call:

def catch(callme, catchme, returnme):
   try:
     return callme()
   except catchme:
     return returnme

phone = catch(lambda: addressbook[name], KeyError, "Unknown")
but that's *really* clunky.

I don't really find this function all the clunky.  However, it also does not solve the case of an expensive 'except' clause that Nick pointed out as desirable to avoid evaluating (or, for that matter, an except clause with side effects).

E.g.

  phone = catch(lambda: addressbook[name], KeyError, lookup_from_internet(name))




--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is

to the 21st century what the slave trade was to the 16th.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/