On 14 October 2016 at 14:46, Franklin? Lee <leewangzhong+pyt...@gmail.com>

> On Oct 14, 2016 9:14 AM, "Gustavo Carneiro" <gjcarne...@gmail.com> wrote:
> >
> > Sorry if I missed the boat, but only just now saw this PEP.
> >
> > Glancing through the PEP, I don't see mentioned anywhere the SQL
> alternative of having a coalesce() function: https://www.
> postgresql.org/docs/9.6/static/functions-conditional.
> >
> > In Python, something like this:
> >
> > def coalesce(*args):
> >     for arg in args:
> >         if arg is not None:
> >              return arg
> >     return None
> >
> > Just drop it into builtins, and voila.   No need for lengthy discussions
> about which operator to use because IMHO it needs no operator.
> >
> > Sure, it's not as sexy as a fancy new operator, nor as headline
> grabbing, but it is pretty useful.
> That function is for a different purpose. It selects the first non-null
> value from a flat collection. The coalesce operators are for traveling down
> a reference tree, and shortcutting out without an exception if the path
> ends.
> For example:
>     return x?.a?.b?.c
>From what I can read in the PEP, it attacks 3 different problems at once:

1. The " null -coalescing" operator is a binary operator that returns its
> left operand if it is not null . Otherwise it returns its right operand.
> 2. The " null -aware member access" operator accesses an instance member
> only if that instance is non- null . Otherwise it returns null . (This is
> also called a "safe navigation" operator.)
> 3. The " null -aware index access" operator accesses an element of a
> collection only if that collection is non- null . Otherwise it returns null
> . (This is another type of "safe navigation" operator.)

I am proposing a coalesce() function as alternative for (solely) problem 1,
while you are talking about problem 2.

I do believe problems 2 and 3 are interesting too, and of course coalesce()
does not work for them, they do need their own operators.

Sorry, I was a bit confused by the PEP attacking 3 (related) problems at


Gustavo J. A. M. Carneiro
Gambit Research
"The universe is always one step beyond logic." -- Frank Herbert
Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to