[Python-ideas] except expression
Andrew Barnert
abarnert at yahoo.com
Thu Feb 13 20:17:45 CET 2014
On Feb 13, 2014, at 11:04, Nathan Schneider <nathan at cmu.edu> wrote:
> On Thu, Feb 13, 2014 at 1:47 PM, spir <denis.spir at gmail.com> wrote:
>> On 02/13/2014 07:43 PM, Amber Yust wrote:
>>> 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
>>
>> I like it. Especially because 'try' already works with 'except'. (But note that 'try', like my proposal of 'then', normally introduces a block).
>
> This strikes me as counterintuitive because it is inconsistent: 'bar()' is being tried, but does not follow 'try', while the others do. And then the 'try None' has no corresponding 'except'.
>
> Suggestion: an expression like
>
> foo = (try bar() except BarException)
>
> that defaults to None if the exception is caught. This could then be chained with 'or':
>
> foo = (try bar() except BarException) or (try baz() except BazException)
But what if bar() can successfully return None, or just a falsey value in general?
Note that this is exactly the reason we needed the if expression: because or is tempting but incorrect in such cases.
>
> as distinguished from
>
> foo = (try bar() except BarException) or baz()
>
> which does not do any exception handling for baz().
>
> (Apologies if something like this has been proposed above; I could not find it from skimming the thread.)
>
> Nathan
>
>>
>> d
>> _______________________________________________
>> 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/
>
> _______________________________________________
> 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/cee72d98/attachment-0001.html>
More information about the Python-ideas
mailing list