[Python-ideas] Null coalescing operator

David Mertz mertz at gnosis.cx
Wed Nov 2 16:50:14 EDT 2016


Even though I really don't want new null-coalescing operators, I really
appreciate the ternary operator in Python (or in C).

On Wed, Nov 2, 2016 at 12:38 PM, Mikhail V <mikhailwas at gmail.com> wrote:

> result = a > b ? x : y
>
> is IMHO a syntactical herecy. Such things disgust me from programming.
> Why on earth one cannot just wrap it in function
> c = nicefunc(a,b)
>

The problem here is that the general form isn't ONLY to return 'x' or 'y'
but the decide between arbitrary values.  Hard-coding the variables into
the function loses 90%+ of the point.

So the general function would need a signature like:

    c = nicefunc(a, b, x, y)

The problem here is that this call might be:

    c = nicefunc(a, b, run_for_hours(), has_side_effects())

We only want ONE of 'x' and 'y' to eagerly evaluate.  In the C or Python
ternary we get exactly that.  Obviously, that also happens in your fully
spelled out if/else block too, but that's multiline and needs to setup
variables not just be used as an expression.


-- 
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/20161102/1a2a0585/attachment-0001.html>


More information about the Python-ideas mailing list