[Python-ideas] Fwd: Null coalescing operator

Rob Cliffe rob.cliffe at btinternet.com
Mon Sep 12 10:03:20 EDT 2016



On 12/09/2016 11:01, Ivan Levkivskyi wrote:
>
>
> On 12 September 2016 at 09:05, Michel Desmoulin 
> <desmoulinmichel at gmail.com <mailto: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
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 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/20160912/5d88a6bc/attachment-0001.html>


More information about the Python-ideas mailing list