[Python-ideas] Using "||" (doubled pipe) as the null coalescing operator?

Erik python at lucidity.plus.com
Fri Sep 25 00:28:45 CEST 2015


On 24/09/15 04:00, Nick Coghlan wrote:
>  data = data if ? is not sentinel else default()

This reads OK in a short example like this and when using word-based 
operators such as "is not". However, it's a bit clumsy looking when 
using operators spelled with punctuation:

data = data if ? != None else default()
data = data if foo <= ? <= bar else default()

 > title = user_title if ? is not None else local_default_title if ? is 
not None else global_default_title

I don't think I like the way '?' changes its target during the line in 
this example.

For example, the equivalent of the admittedly-contrived expression:

foo = bar if foo is None else baz if baz is not None else foo.frobnicate()

is:

foo = bar if ? is None else baz if ? is not None else foo.frobnicate()

... so you still have to spell 'foo' repeatedly (and only due to the 
subtle switch of the '?' target, which might go away (or be added) 
during code maintenance or refactoring).


Also, if '?' is sort of a short-cut way of referencing the LHS, then one 
might naively expect to be able to write this:

[(x, y) for x in range(5) if ? < 3 for y in range(5) if ? > 2]

Regs, E.


More information about the Python-ideas mailing list