[Python-ideas] Null coalescing operators

C Anthony Risinger anthony at xtfx.me
Sat Sep 19 10:17:07 CEST 2015

On Fri, Sep 18, 2015 at 11:21 PM, Guido van Rossum <guido at python.org> wrote:

> On Fri, Sep 18, 2015 at 8:41 PM, Steven D'Aprano <steve at pearwood.info>
> wrote:
>> It's a funny thing, I'm usually not a huge fan of symbols outside of
>> maths operators, and I strongly dislike the C ? ternary operator, but
>> this one feels really natural to me. I didn't have even the most
>> momentary "if you want Perl, you know where to find it" thought.
> I do, but at least the '?' is part of an operator, not part of the name
> (as it is in Ruby?).
> I really, really, really don't like how it looks, but here's one thing:
> the discussion can be cut short and focus almost entirely on whether this
> is worth making Python uglier (and whether it's even ugly :-). The
> semantics are crystal clear and it's obvious that the way it should work is
> by making "?.", ?(" and "?[" new operators or operator pairs -- the "?"
> should not be a unary postfix operator but a symbol that combines with
> certain other symbols.

I really liked this whole thread, and I largely still do -- I?think -- but
I'm not sure I like how `?` suddenly prevents whole blocks of code from
being evaluated. Anything within the (...) or [...] is now skipped (IIUC)
just because a `?` was added, which seems like it could have side effects
on the surrounding state, especially since I expect people will use it for
squashing/silencing or as a convenient trick after the fact, possibly in
code they did not originally write.

If the original example included a `?` like so:

    response = json.dumps?({
        'created': created?.isoformat(),
        'updated': updated?.isoformat(),

should "dumps" be None, the additional `?` (although though you can barely
see it) prevents *everything else* from executing. This may cause confusion
about what is being executed, and when, especially once nesting (to any
degree really) and/or chaining comes into play!

Usually when I want to use this pattern, I find I just need to write things
out more. The concept itself vaguely reminds me of PHP's use of `@` for
squashing errors. In my opinion, it has some utility but has too much
potential impact on program flow without being very noticeable. If I saw
more than 1 per line, or a couple within a few lines, I think my ability to
quickly identify -> analyze -> comprehend possible routes in program
control flow decreases. I feel like I'll fault more, double back, and/or
make sure I forevermore look harder for sneaky `?`s.

I probably need to research more examples of how such a thing is used in
real code, today. This will help me get a feel for how people might want to
integrate the new `?` capability into their libraries and apis, maybe that
will ease my readability reservations.



C Anthony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150919/d076c13e/attachment-0001.html>

More information about the Python-ideas mailing list