[Python-ideas] except expression
David Mertz
mertz at gnosis.cx
Thu Feb 13 19:31:28 CET 2014
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140213/d0e61135/attachment.html>
More information about the Python-ideas
mailing list