[Python-ideas] PEP 505 (None coalescing operators) thoughts

Bruce Leban bruce at leban.us
Thu Oct 1 04:53:26 CEST 2015


I don't understand the resistance to using ?? ?. ?[] ?() and the quest to
contort some English words into this. The concepts "or" and "and" are
natural language concepts and "if null" is not. The goal here should be
readability and sometimes that readability is enhanced by being slightly
different than other programming languages and sometimes it's

For example, Java using -> for lambda is confusing to someone coming from
C++ where -> means de-reference. C# avoided that confound by using =>. I'm
sure someone thought that -> was more Javalike.

Very few people writing/reading Python only use Python. There is a strong
benefit to being consistent with other languages when there's not "one
obvious" way to do it in "more Pythonic" syntax. So far, *none* of the
alternatives are easier to read (for my taste) than the ? operators.

As to the people that are questioning the need for any of this, I challenge
you to look through your projects for code like this:

Python:  \bif\b.*\bis *(not *)?none *else
C-like:  ([=!]= *null\b|\bnull *[=!]=) *\?

and see how many hits you find. If you want to avoid using None (or null)
in your code, that's fine but the reality is that None is used extensively
*and* there are *many* cases where we do exactly what these operators
simplify.


On Wed, Sep 30, 2015 at 6:08 PM, MRAB <python at mrabarnett.plus.com> wrote:

>
>> It's only just occurred to me that there's a small inconsistency here.
> The "?.", "?[" and "?(" will short-circuit on None, whereas the "??"
> will short-circuit on non-None.
>
> Would that cause any confusion in practice?


Personally, I don't think so and I never thought about this as an area of
confusion. In the ?? case, it's obvious


Two more notes:

(1) If we have ?. ?() ?[] then spelling the ?? operator with a ? makes a
lot of sense but mixing a word and an operator (like or?) just looks ugly
to me. That said, if Guido declares absolutely no on ?. ?() ?[] and yes on
?? but it has to be spelled with words, my preference would be "*or else*".
It doesn't really say "if is None" to me but (i) it's a syntax error right
now; and (ii) the else in there implies some if-like logic is going on
rather than just or-like logic:

(foo or else bar) == (foo if foo is not None else bar).

(2) As to ?= or ??=, I am less enamored of that but not because that case
never occurs. I often find that the initialization code after if foo is
null: is more than a single expression and therefore not amenable to ?=.
That is, I don't just want to create an object and assign to the variable,
I want to make initialization calls to it.


--- Bruce
Check out my new puzzle book: http://J.mp/ingToConclusions
<http://j.mp/ingToConclusions>
Get it free here: http://J.mp/ingToConclusionsFree
<http://j.mp/ingToConclusionsFree> (available on iOS)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150930/9520d66e/attachment.html>


More information about the Python-ideas mailing list