[Python-ideas] Null coalescing operators

Erik python at lucidity.plus.com
Sat Sep 19 00:56:40 CEST 2015

On 18/09/15 23:37, Chris Angelico wrote:
> Python generally doesn't special-case None, so having a bit of magic
> that works only on that one object seems a little odd.

So the answer here is to introduce a "magic" hook that None can make use 
of (but also other classes). I can't think of an appropriate word, so 
I'll use "foo" to keep it suitably abstract.

If the foo operator uses the magic method "__foo__" to mean "return an 
object to be used in place of the operand should it be considered ... 
false? [or some other definition - I'm not sure]" then any class can 
implement that method to return an appropriate proxy object.

If that was a postfix operator which has a high precedence, then:

bar = foo?

and the original syntax suggestion:

bar = foo?.isoformat()

... are equivalent. "?." is not a new operator. "?" is. This is 
essentially a slight refinement of Chris's case 3 -

> 3) Add another case: func?(args) evaluates func, and if it's None,
> evaluates to None without calling anything.
> Option 3 requires a bit more protection, but is completely explicit.
> It would also have use in other situations. Personally, I support that
> option; it maintains all the identities, is explicit that calling None
> will yield None, and doesn't need any magic special cases. It does add
> another marker, though:


More information about the Python-ideas mailing list