[Python-ideas] Null coalescing operator

Nick Badger nbadger1 at gmail.com
Wed Oct 26 15:41:10 EDT 2016


Am Freitag, 14. Oktober 2016 23:11:48 UTC-7 schrieb Nick Coghlan:
>
>
> Regarding the spelling details, my current preferences are as follows: 
>
> * None-coalescing operator: x ?or y 
> * None-severing operator: x ?and y 
> * None-coalescing augmented assignment: x ?= y 
> * None-severing attribute access: x?.attr 
> * None-severing subscript lookup: x?[expr] 
>  
>

This is, more or less, the syntax added in Nick's PEP 531 draft 
<https://www.python.org/dev/peps/pep-0531/>. The reddit discussion 
<https://www.reddit.com/r/Python/comments/59d2he/pep_531_existence_checking_operators/> 
about it raised some pretty major concerns about clarity, and I have to 
admit, I think if you're learning Python as a first language, the ?and, 
?else, x?.attr, etc syntax is likely to be very confusing. For me 
personally, combining a new operator "?" with existing keywords like "and" 
or "else" just does not make any intuitive sense. I definitely see the 
value, though, in particular of None-severing, especially as a tool to 
explicitly specify which attr can be missing -- ie, disambiguating which 
attribute is missing in a foo.bar.baz lookup (the alternative to which is 
nested try: except AttributeError: blocks, which gets very messy very 
quickly). I'm on board with the idea, and I can absolutely imagine using it 
in my code, but I disagree on the spelling.

A thought I had (perhaps more readable in a reddit comment 
<https://www.reddit.com/r/Python/comments/59d2he/pep_531_existence_checking_operators/d98rnbu/>) 
is to condense everything into a single "?" symbol, used for:

+ Coalescing binary operator: foo ? bar
+ Coalescing augmented assignment operator: foo ?= bar
+ Severing unary operator: ?foo

*Pseudocode binary operator examples:*

>>> foo_exists ? bar_never_evaluated
foo_exists

>>> foo_missing ? foo_exists
foo_exists

>>> foo_missing ? bar_missing
foo_missing


*Pseudocode augmented examples:*

>>> foo_exists = 'foo'
>>> foo_exists ?= bar_never_evaluated
>>> foo_exists == 'foo'
True

>>> foo = Missing
>>> bar_exists = 'bar'
>>> foo ?= bar_exists
>>> foo == 'bar'
True

>>> foo = None
>>> bar_missing = Missing
>>> foo ?= bar_missing
>>> foo == None
True


*Pseudocode unary examples:*

>>> ?(foo_exists).bar.baz
foo_exists.bar.baz
>>> ?(foo_exists)[bar][baz]
foo_exists[bar][baz]

>>> ?(foo_missing).bar.baz
Missing
>>> ?(foo_missing)[bar][baz]
Missing

>>> ?(foo_exists).bar.baz_missing
Traceback...
AttributeError: <foo_exists.bar> object has no attribute 'baz_missing'
>>> ?(foo_exists)[bar][baz_missing]
Traceback...
KeyError: 'baz_missing'

>>> ?(foo_missing).bar.baz_missing
Missing
>>> ?(foo_missing)[bar][baz_missing]
Missing


I personally think that's substantially more readable, but I suppose that's 
at least somewhat a matter of personal preference.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20161026/847b98ba/attachment-0001.html>


More information about the Python-ideas mailing list