[Python-ideas] Null coalescing operator

MRAB python at mrabarnett.plus.com
Fri Sep 9 16:21:16 EDT 2016


On 2016-09-09 21:01, Arek Bulski wrote:
> Sometimes I find myself in need of this nice operator that I used back
> in the days when I was programming in .NET, essentially an expression
>
>>>> expr ?? instead
>
> should return expr when it `is not None` and `instead` otherwise.
>
> A piece of code that I just wrote, you can see a use case:
>
>     def _sizeof(self, context):
>         if self.totalsizeof is not None:
>             return self.totalsizeof
>         else:
>             raise SizeofError("cannot calculate size")
>
> With the oprator it would just be
>
>     def _sizeof(self, context):
>         return self.totalsizeof ?? raise SizeofError("cannot calculate
> size")

'raise' is a statement, so it can't appear in an expression.

This has been discussed before, so you might want to read this thread first:

Null coalescing operators
https://mail.python.org/pipermail/python-ideas/2015-September/036289.html



More information about the Python-ideas mailing list