[Python-ideas] except expression

Amber Yust amber.yust at gmail.com
Sat Feb 15 23:22:15 CET 2014


I don't think adding multiple elements of syntax for an incomplete solution
makes sense.
On Feb 15, 2014 4:09 PM, "Bruce Leban" <bruce at leapyear.org> wrote:

> While I'm generally positive about the idea of an except expression (and
> favor A except E pass B over any of the alternatives), I think it's worth
> looking in a different direction.
>
> One of the motivations of this idea is that there are some use cases that
> are particularly inconvenient. Another is the proliferation of APIs
> accepting default values.
>
> So instead of a general solution, let me suggest an alternative that
> addresses specific use cases. I'll write these in terms of an except
> expression so that they can be directly compared head to head.
>
> a[?b]     <=>   a[b] except (IndexError, TypeError)pass None
> a.?b      <=>   a.b except AttributeError pass None
> a(?b)     <=>   a(b) except Exception pass None
> a or? b   <=>   (a except Exception pass None) or b
> a and? b  <=>   (a except Exception pass None) and b
>
> del? a.b  <=>  try:
>                    del a.b
>                except AtributeError:
>                    pass
>
> Of course you can chain these:
>
> a[?b][?c]
> a.?b.?c
>
> a or? b or? c
>
>
> Pros/Cons:
> - no option to specify replacement value on an exception
> - no option to specify exceptions to catch
> - doesn't specifically solve the api default value issue -- and the option
> for that case a(?b) catches all exceptions rather than specific ones
>
>
> --- Bruce
> Learn how hackers think: http://j.mp/gruyere-security
>
> Note: the syntax could just as easily be a?[b], a?.b, etc.
>
> _______________________________________________
> 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/20140215/2642864b/attachment-0001.html>


More information about the Python-ideas mailing list