[Python-Dev] PEP 463: Exception-catching expressions
Thomas Wouters
thomas at python.org
Sun Feb 23 20:26:21 CET 2014
On Feb 23, 2014 7:52 PM, "Stefan Behnel" <stefan_ml at behnel.de> wrote:
>
> Chris Angelico, 21.02.2014 04:15:
> > Just as PEP 308 introduced a means of value-based conditions in an
> > expression, this system allows exception-based conditions to be used
> > as part of an expression.
> > [...]
> > This currently works::
> >
> > lst = [1, 2, None, 3]
> > value = lst[2] or "No value"
> >
> > The proposal adds this::
> >
> > lst = [1, 2]
> > value = lst[2] except IndexError: "No value"
>
> I see a risk of interfering with in-place assignment operators, e.g.
>
> x /= y except ZeroDivisionError: 1
>
> might not do what one could expect, because (as I assume) it would behave
> differently from
>
> x = x / y except ZeroDivisionError: 1
Yes. Augmented assignment is still assignment, so a statement. The only way
to parse that is as
x /= (y except ZeroDivisionError: 1)
and it'd be equivalent to
x = x / (y except ZeroDivisionError: 1)
(If the parentheses are mandatory that makes it easier to spot the
difference.)
>
> I think that falls under the "overly broad exception handling" issue. If
> you want to include the assignment, you'll have to spell out the
try-except
> block yourself. I find the difference in the two behaviours very
> unfortunate, though.
>
> This also reduces the scope of applicability somewhat. Cython has typed
> assignments, so a straight forward idea would be to handle TypeErrors in
> assignments like this:
>
> cdef str s
> s = x except TypeError: str(x)
>
> However, I guess that would similarly counter the idea of exception
> handling in an *expression*, and the correct and non-ambiguous way to do
> this would be to spell out the try-except block.
>
> Summing it up, my impression is that it helps some use cases but leaves
> others more ambiguous/unclear/unfortunate, which makes me lean towards
> rejecting it.
>
> Stefan
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
https://mail.python.org/mailman/options/python-dev/thomas%40python.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20140223/cd9bd0fb/attachment-0001.html>
More information about the Python-Dev
mailing list