[Python-ideas] PEP 505: None-aware operators

Paul Moore p.f.moore at gmail.com
Thu Jul 19 09:20:14 EDT 2018


On 19 July 2018 at 13:39, Rhodri James <rhodri at kynesim.co.uk> wrote:

>> After updating to use the ``?[]`` and ``??`` operators::
>>
>>      def _get_const_info(const_index, const_list):
>>          argval = const_list?[const_index] ?? const_index
>>          return argval, repr(argval)
>
>
> Here's where I start to part company.  To me, the updated version is
> markedly harder to read, even if it does (once deciphered) convey the intent
> of the function better than the original :-)  The "?." and "?[]" operators
> just aren't obvious enough not to trip my internal WTF filter; either that
> or I'll overlook the "?" part entirely, which is probably worse.

I completely agree. The semantics of ?. and ?[] is non-intuitive at
best - and particularly when chained. The Groovy language has the ?.
operator (see http://groovy-lang.org/operators.html#_safe_navigation_operator),
and I pretty much always read it as if it were the equivalent
expression without the ? signs, with an added proviso "it'll probably
do something useful if we hit None". But when it comes to what that
"something useful" is, I'm left hoping the original writer knew what
they were doing.

So I'd never write code using ?. or ?[] myself, and I'd be unable to
reasonably review or maintain code containing them. That's a pretty
serious condemnation of the syntax in my view.

Conversely, while I find ?= and ?? ugly, and would avoid them, the
semantics are relatively easy to assess when reading code. Of the two,
I dislike ?? more than ?=, but both are streets ahead of ?. and ?[].

Paul


More information about the Python-ideas mailing list