[Python-ideas] Null coalescing operators

Andrew Barnert abarnert at yahoo.com
Sat Sep 19 03:03:15 CEST 2015


On Sep 18, 2015, at 16:58, Sven R. Kunze <srkunze at mail.de> wrote:
> 
>> On 19.09.2015 01:47, Ryan Gonzalez wrote:
>> What about "apply"? It's the closest thing to "fmap" I can think of that won't coblnfuse people...
> 
> Are you sure? I think "maybe" better reflects the purpose of "?".
> 
> 
> Nevertheless, I would love to see support for the maybe monad in Python.

I think this, and the whole discussion of maybe and fmap, is off the mark here.

It's trivial to create a maybe type in Python.

What's missing is the two things that make it useful: (1) pattern matching, and (2) a calling syntax and a general focus on HOFs that make fmap natural. Without at least one of those, maybe isn't useful. And adding either of those to Python is a huge proposal, much larger than null coalescing, and a lot less likely to gain support.

Also, the monadic style of failure propagation directly competes with the exception-raising style, and they're both contagious. A well-designed language and library can have both side by side if it, e.g., rigorously restricts exceptions to only truly exceptional cases, but the boat for that sailed decades ago in Python. So just having them side by side would lead to the exact same problems as C++ code that mixes exception-based and status-code-based APIs, or JavaScript code that mixes exceptions and errbacks or promise.fail handlers. 

Personally, whenever I think to myself "I could really use maybe here" in some Python code, that's a sign that I'm not thinking Pythonically, and either need to switch gears in my brain or switch languages. Just like when I start thinking about how I could get rid of that with statement with an RAII class, and maybe an implicit conversion operator....


More information about the Python-ideas mailing list