[Python-ideas] Null coalescing operators

Sven R. Kunze srkunze at mail.de
Mon Sep 21 06:52:13 CEST 2015

On 21.09.2015 04:35, Mark E. Haase wrote:
> A) Is coalesce a useful feature? (And what are the use cases?)

I limit myself to materializing default arguments as in:

def a(b=None):
     b = b or {}

Because its a well known theme (and issue) of the mutability of default 
arguments of Python.

> B) If it is useful, is it important that it short circuits? (Put 
> another way, could a function suffice?)
> C) If it should be an operator, is "??" an ugly spelling?
>     >>> retries = default ?? cls.DEFAULT

The only difference between "or" and "??" is that "??" is None only, 
right? At least to me, the given use case above does not justify the 
introduction of "??".

> D) If it should be an operator, are any keywords more aesthetically 
> pleasing? (I expect zero support for adding a new keyword.)
>     >>> retries = default else cls.DEFAULT
>     >>> retries = try default or cls.DEFAULT
>     >>> retries = try default else cls.DEFAULT
>     >>> retries = try default, cls.DEFAULT
>     >>> retries = from default or cls.DEFAULT
>     >>> retries = from default else cls.DEFAULT
>     >>> retries = from default, cls.DEFAULT
> My answers:
> A) It's useful: supplying default instances for optional values is an 
> obvious and common use case.

Yes, "or" suffices in that case.

> B) It should short circuit, because the patterns it replaces (using 
> ternary operator or "or") also do.

They look ugly and unpleasant because they remind you to reduce the 
usage of None; not to make dealing with it more pleasant.

> C) It's too restrictive to cobble a new operator out of existing 
> keywords; "??" isn't hard to read when it is separated by whitespace, 
> as Pythonistas typically do between a binary operator and its operands.
> D) I don't find any of these easier to read or write than "??".

"or" is easier to type (no special characters), I don't need to explain 
it to new staff, and it's more pleasant to the eye.

I remember my missis telling me, after I showed her some C# code, that 
programmers tend to like weird special characters. Well, that might 
certainly be true. Special characters increase the visual noise and the 
mental strain when reading. They make the lines they are in special. I 
don't see anything special with "or" and with the single use case I have 
for it. :)


More information about the Python-ideas mailing list