[Python-ideas] [Python-Dev] What's the status of PEP 505: None-aware operators?

Steven D'Aprano steve at pearwood.info
Wed Nov 29 01:08:45 EST 2017


On Tue, Nov 28, 2017 at 12:31:06PM -0800, Raymond Hettinger wrote:
> 
> > I also cc python-dev to see if anybody here is strongly in favor or against this inclusion.
> 
> Put me down for a strong -1.  The proposal would occasionally save a 
> few keystokes but comes at the expense of giving Python a more Perlish 
> look and a more arcane feel.

I think that's an unfair characterisation of the benefits of the PEP. 
It's not just "a few keystrokes".

Ironically, the equivalent in Perl is // which Python has used for 
truncating division since version 2.4 or so. So if we're in danger of 
looking "Perlish", that ship has sailed a long time ago.

Perl is hardly the only language with null-coalescing operators -- we 
might better describe ?? as being familiar to C#, PHP, Swift and Dart. 
That's two mature, well-known languages and two up-and-coming languages.


[...]
>     timeout ?? local_timeout ?? global_timeout

As opposed to the status quo:

    timeout if timeout is not None else (local_timeout if local_timeout is not None else global_timeout)

Or shorter, but even harder to understand:

    (global_timeout if local_timeout is None else local_timeout) if timeout is None else timeout

I'd much prefer to teach the version with ?? -- it has a simple 
explanation: "the first of the three given values which isn't None". The 
?? itself needs to be memorized, but that's no different from any other 
operator. The first time I saw ** I was perplexed and couldn't imagine 
what it meaned.

Here ?? doesn't merely save a few keystrokes, it significantly reduces 
the length and complexity of the expression and entirely cuts out the 
duplication of names.

If you can teach 

    timeout or local_timeout or global_timeout

then you ought to be able to teach ??, as it is simpler: it only 
compares to None, and avoids needing to explain or justify Python's 
truthiness model.


>     'foo' in (None ?? ['foo', 'bar'])

If you can understand

    'foo' in (False or ['foo', 'bar'])

then surely you can understand the version with ??.


>     requested_quantity ?? default_quantity * price

Again:

    (default_quantity if requested_quantity is None else requested_quantity) * price


I'd much prefer to read, write and teach the version with ?? over the 
status quo.


-- 
Steve


More information about the Python-ideas mailing list