[Python-ideas] Fwd: Null coalescing operator

Guido van Rossum guido at python.org
Sat Sep 10 19:44:54 EDT 2016


There seems to be a major misunderstanding here. A None-coalescing
operator is not for catching AttributeError, it's a shortcut similar
to "a or b" except that it checks for "a is None" rather than bool(a).

On Sat, Sep 10, 2016 at 4:38 PM, David Mertz <mertz at gnosis.cx> wrote:
> 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.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/



-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list