Assuming you can't break existing code that already traps TypeError, AttributeError, etc., I don't see how you can do this without
On 12 September 2016 at 09:05, Michel Desmoulin <desmoulinmichel@gmail.com> wrote:
In the form of:
val = do_thing() except ThingError: "default"
[...]
But it also can deal with many common operations in Python without the
need to add more operators or variants:
val = my_list[0] except IndexError: "default"
val = iterable[0] except TypeError: next(iter(iterable))
val = int(param) except ValueError: man.nan
I like this idea, I would propose a (maybe crazy) addition to it. What about a special exception NoneError, that will catch TypeError, AttributeError etc. but only when it was caused by None(),
None.attr, None[1], etc. With this one can write:
x = a.b()[0] except NoneError: 'default'
without a risk of catching other (unrelated) exceptions.
--
Ivan
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/