[Python-ideas] Fwd: Null coalescing operator

David Mertz mertz at gnosis.cx
Sat Sep 10 19:38:32 EDT 2016


Sorry, I sent this accidentally as private reply, then tried to fix it on
phone.  The latter produced horrible formatting.  Please just read this
version.

On Sat, Sep 10, 2016 at 4:10 PM, Guido van Rossum <guido at python.org> wrote:

> So you're offering `NoneCoalesce(x).bar` as less-ugly alternative to
> `x?.bar`... Color me unconvinced.
>

No, I'm offering a more realistic use pattern:

for x in get_stuff():

    x = NoneCoalesce(x)

    # ... bunch of stuff with x ...
    # ... more stuff with nested keys or attributes ...

    x2 = x.foo

    x3 = x.bar.baz[x2]

    x4 = x(x.val)

    result = x3(x4)



As a less ugly alternative in the fairly uncommon case that you want None
coalescing as the behavior of getting attributes, keys, call values, etc.
that may or may not be available (AND where you don't want to wrap all of
those access patterns in one try/except block).

In contrast, the ugly version of even this pretty simple toy code with the
hypothetical syntax would be:

for x in get_stuff():

    # ... bunch of stuff with x ...

    # ... more stuff with nested keys or attributes ...

    x2 = x?.foo

    x3 = x?.bar?.baz?[x2]

    x4 = x?(x?.val)

    result = x3?(x4)


This second case looks absolutely awful to me.  And real world uses, if
implemented, would quickly get much worse than that.

Yours, David...

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160910/ae34431c/attachment.html>


More information about the Python-ideas mailing list