[Python-ideas] except expression

Nick Coghlan ncoghlan at gmail.com
Thu Feb 13 10:28:49 CET 2014


On 13 Feb 2014 19:24, "Nick Coghlan" <ncoghlan at gmail.com> wrote:
>
> General comment: like Raymond, I'm inclined to favour a nice expression
friendly exception handling syntax, precisely because of the proliferation
of relatively ad hoc alternative solutions (in particular, the popularity
of being able to pass in default values to handle empty iterables).
>
> One use case, for example, is handing IndexError when retrieving an item
from a sequence (which currently has no nice standard spelling, and isn't
amenable to the "pass in a default answer" solution because it isn't a
normal function call).
>
> Another case not handled well by the status quo is when the default
answer is expensive to calculate for some reason, so you really only want
to calculate it if you actually need it.
>
> Unfortunately, like PEP 308 before it, the hard part is coming up with a
reasonable spelling that won't have people breaking out the torches and
pitchforks if the PEP is accepted.
>
> On 13 Feb 2014 13:34, "Amber Yust" <amber.yust at gmail.com> wrote:
> >
> > Another possible option:
> >
> > foo = something() except None for BarException
> >
> > With possible support for:
> >
> > foo = something() except e.message for BarException as e
>
> This is also one of the possible spellings I came up with, and it is my
current least disliked option. The main concern I have with it is the
substantially different interpretation it gives to the "for" keyword - as
far as is practical, we try to ensure that a given keyword relates to a
consistent concept, and the link to iteration is rather tenuous here (it's
only present in the fact you can use an iterable of exception types rather
than just one). Aside from that concern, I think it scores well on the
readability front.
>
> "if" would be better, but, as you already noted, poses significant
parsing challenges (since it likely wouldn't be easy to require that
ternary expressions use parentheses in this new construct, but still allow
the parentheses to be omitted in the general case).

"from" is another keyword choice worth considering here - that already has
no strong semantics of its own ("from x import y" and "yield from iter"
derive their meaning from the other keyword involved)

Cheers,
Nick.

>
> Cheers,
> Nick.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140213/730ee2a7/attachment.html>


More information about the Python-ideas mailing list