[Python-ideas] chained try (was Re: "maybe import"?)

Nick Coghlan ncoghlan at gmail.com
Mon Dec 30 01:24:13 CET 2013


On 30 Dec 2013 08:24, "Andrew Barnert" <abarnert at yahoo.com> wrote:
>
> On Dec 29, 2013, at 1:41, spir <denis.spir at gmail.com> wrote:
>
> >> "except try" looks horrible, but I can't think of anything better.
> >
> > I would buy (if cheap!):
> >
> >    try:
> >        stuff
> >    else try:
> >        other stuff
> >    else try:
> >        different stuff
> >    except Exception as e:
> >        deal with it
>
> The advantage of "except try" is that it leaves the door open for
specifying only certain exceptions instead of all, which would be useful in
some cases.
>
> But the advantage of "else try" is that it closes that door, eliminating
most of the other questions I and others raised. It's clearly pure
syntactic sugar with no ambiguity. Maybe that's a better idea.

The else block triggers when no exception was thrown, so even if this idea
was to be accepted at all (which seems unlikely), that particular spelling
definitely wouldn't be accepted for something that only triggers when an
exception is thrown.

In practice, the existing more general solution of extracting an
appropriately named function is likely to improve readability more than
adding a new variation on the already complex try/except/else/finally
construct, which is why I consider it unlikely that a change along these
lines would be accepted.

Cheers,
Nick.

> _______________________________________________
> 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/20131230/e492a4e5/attachment.html>


More information about the Python-ideas mailing list