[Python-ideas] except expression

Amber Yust amber.yust at gmail.com
Thu Feb 13 19:43:59 CET 2014


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 at gnosis.cx> wrote:

> On Thu, Feb 13, 2014 at 3:25 AM, spir <denis.spir at 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 at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140213/dc8077c3/attachment.html>


More information about the Python-ideas mailing list