[Python-ideas] Fwd: Null coalescing operator

Ivan Levkivskyi levkivskyi at gmail.com
Mon Sep 12 06:01:53 EDT 2016


On 12 September 2016 at 09:05, Michel Desmoulin <desmoulinmichel at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160912/8a95359a/attachment-0001.html>


More information about the Python-ideas mailing list