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

Terry Reedy tjreedy at udel.edu
Thu Jul 19 14:47:11 EDT 2018


On 7/19/2018 8:46 AM, Rhodri James wrote:
> On 19/07/18 09:33, Antoine Pitrou wrote:
>> There is a use case I sympathize with: the argument-is-None case.  For
>> that I would suggest a simpler form:  "A else B" which would evaluate
>> to A if A is not None, otherwise to B (parentheses may be mandatory).
>>
>> So e.g. one of the examples would read:
>>
>>       def insort_right(a, x, lo=0, hi=None):
>>           # ...
>>           hi = hi else len(a)
>>           # ...
> 
> Much as I would like a keyword, "else" is the wrong one.  It implies we 
> are dealing with truthiness, which we aren't,

'A else B' is about the truthiness of 'A is not None', just as
'A or B' is about the truthiness of 'bool(a) is not False'.
  See my response to Antoine.

A else B' would abbreviate 'tem = A; tem if tem is not None else B'

> and lays a subtle semantic trap as a consequence.

I think most anyone should be able to get that 'A else B' is similar to 
'A or B' but must be different.

> If anyone can think of a good word for "if it isn't None, otherwise", 
> I'd be all for it :-)
-- 
Terry Jan Reedy




More information about the Python-ideas mailing list