[Python-ideas] Null coalescing operator

Barry Warsaw barry at python.org
Mon Oct 17 18:45:56 EDT 2016


On Oct 15, 2016, at 04:10 PM, Nick Coghlan wrote:

>Having been previously somewhere between -1 and -0, I've been doing a
>lot more data mining and analysis work lately, which has been enough
>to shift me to at least +0 and potentially even higher when it comes
>to the utility of adding these operators (more on that below).

I'm sympathetic to (some of) the goals of PEP 505, as these issues do
occasionally annoy me.  But I'm not entirely convinced they are common enough
or annoying enough to warrant special syntax, and I *really* dislike the
introduction of a ? operator for these purposes.  I'm also concerned about
adopting too much generality muddling up what I think should be a narrowly
targeted improvement to readability.

The other thing to note is that, while I often use ternary operators for this
now, checking against None isn't always the sole conditional.  E.g.

        self.chain = (chain
                      if chain is None or IChain.providedBy(chain)
                      else config.chains[chain])

That being said, null-aware member access (NAMA) would be pretty handy
occasionally.  I'm less sure about the other forms.  For me, the biggest
benefit of NAMA is the short-circuiting of chained attribute access.

I don't like the operator syntax because I find it less readable (harder for
the eye to pick out), and because it isn't a completely obvious operation.
But also because I generally want to chase the attributes all-or-nothing.  For
example, foo.bar.baz.qux but only if all the intermediary attributes resolve
to non-Nones.  I don't want to have to write foo.?bar.?baz.?qux

I tried playing around with new keywords such as 'when' and 'unless', which
seem a little nice although not a perfect fit.

    thing = foo.bar.baz.qux unless None

    thing = unless None then foo.bar.baz.qux

    thing = when foo.bar.baz.qux

    thing = foo.bar.baz.qux when not None

I do like the idea of a keyword more than an operator, and disagree that a new
keyword can't be introduced until Python 4.  That's why we have __future__!

Anyway, that's my $0.02.  I trust Guido to DTPT (do the Pythonic thing :),
even if that means rejecting the PEP.

Cheers,
-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 801 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161017/30855147/attachment.sig>


More information about the Python-ideas mailing list