On 12/09/2016 11:01, Ivan Levkivskyi wrote:


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
Assuming you can't break existing code that already traps TypeError, AttributeError, etc., I don't see how you can do this without
having separated kinds of NoneError which were subclasses of TypeError, AttributeError, etc.
Rob Cliffe

   



_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/