[Python-ideas] except expression

Bruce Leban bruce at leapyear.org
Sat Feb 15 22:08:24 CET 2014


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140215/4ef15034/attachment.html>


More information about the Python-ideas mailing list