[Python-ideas] Null coalescing operators

Stephen J. Turnbull stephen at xemacs.org
Sat Sep 19 07:14:40 CEST 2015

Andrew Barnert via Python-ideas writes:

 > So, do we need a dunder method for the "?" operator? What else
 > would you use it for besides None?

NaNs in a pure-Python implementation of float or Decimal.  (This is
not a practical suggestion.)

A true SQL NULL type.  It's always bothered me that most ORMs map NULL
to None but there are plenty of other ways to inject None into a
Python computation.  (This probably isn't a practical suggestion
either unless Random832's suggestion of ?() establishing a lexical
context were adopted.)

The point is that Maybe behavior is at least theoretically useful in
subcategories, with special objects other than None.

Sven's suggestion of calling this the "monad" operator triggers a
worry in me, however.  In Haskell, the Monad type doesn't enforce the
monad laws, only the property of being an endofunctor.  That
apparently turns out to be enough in practice to make the Monad type
very useful.  However, in Python we have no way to enforce that
property.  I don't have the imagination to come up with a truly
attractive nuisance here, and this operator doesn't enable general
functorial behavior, so maybe it's not a problem.

More information about the Python-ideas mailing list