[Python-ideas] Null coalescing operator
David Mertz
mertz at gnosis.cx
Fri Sep 9 16:10:03 EDT 2016
This idea has come up before. While I can see the use of it, to me at
least that use doesn't feel nearly common enough to warrant dedicated
syntax.
In many cases, it is a "truthy" value you are looking for rather than `is
not None` specifically. That has a convenient spelling:
expr or instead
If it really is the actual None-ness you are curious about, you need the
slightly longer:
expr if expr is not None else instead
Your example seems to want to fall back to a statement suite rather than a
value. To do that, you'd have to put the suite inside a function such as:
def Raise(err):
raise err
And use it something like:
self.totalsizeof or Raise(SizeofError(...))
On Fri, Sep 9, 2016 at 1:01 PM, Arek Bulski <arek.bulski at gmail.com> 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")
>
>
>
> pozdrawiam,
> Arkadiusz Bulski
>
>
> _______________________________________________
> 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/
>
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160909/2c148fba/attachment-0001.html>
More information about the Python-ideas
mailing list