[Python-ideas] Null coalescing operator

Mikhail V mikhailwas at gmail.com
Wed Nov 2 20:33:40 EDT 2016


On 2 November 2016 at 21:50, David Mertz <mertz at gnosis.cx> wrote:
> 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.

[Apologies for off-topic one more time]

So you say "that's multiline" just as if a multiner is something bad.
All evil comes from the wish to write things more compactly.
Also what to evaluate or not evaluate lies solely on the compiler/interpreter
so that is not what the user must think of.
Anyway, I am too far from doing chain attribute selections, but
for the above example, a one-liner:

    if (a > b) : run_for_hours()  else  has_side_effects()

Is this different from above? For me it is way more readable and no need
to learn/memorise new operators.
Also, *anything* of less then 3 characters as an operator
is most likely to be initially a fail, simply because it is hard to
*see* it in a mess of code, unlike kewords and well formatted multiliner.

Or for example to return non-null of multiple operands:

value = not_a_function_but_selector ( a, b, c, d )

Cannot this principle be exposed to attribute selector?
As said I am too far from these problematics, still I'll try,
though it may be total nonsense I am telling here:

with seek_chain(foo.bar.bee.buzz) :  name = "got it"

I mean do you really need to stick something in the middle of chain,
why not leave the whole thing there and let the "seek_chain"
do the job (whatever this must be, I don't have an idea)

Mikhail


More information about the Python-ideas mailing list