[Python-ideas] Using "||" (doubled pipe) as the null coalescing operator?

Erik python at lucidity.plus.com
Fri Sep 25 04:01:12 CEST 2015


Hi Steven,

On 25/09/15 02:35, Steven D'Aprano wrote:
> I think that your intention is for that to be equivalent to:
>
> if bar not None:  # missing "is" operator
>      foo = bar(param0, param1)
> else:
>      foo = default()

Yes, you are correct. I omitted the 'is'.

> I thought `bar.(<accessor> ...)` meant attribute access, so I initially
> expected the true branch to evaluate to:
>
>      foo = bar.(param0, param1)
>
> which of course is a syntax error. Presumably you would write
> `bar.(attr if ...)` for attribute access and not `bar.(.attr if ...)`.

I chose ".()" on purpose because it was a syntax error. Not including 
the "." meant it looks like a function call, so that wasn't workable.

".()" was supposed to read "I'm doing something with this object, but 
what I'm doing is conditional, so read on".

> I'm still confused about the missing `is`. Maybe you meant:

No, I meant to write 'is'.

> Worse, it's actually ambiguous in some cases:

Hmmm. Yes, OK, I see the problem here.

>> foo = bar.([idx] if != sentinel else default())
>
> I **really** hate this syntax.

"hate" is a very strong word. You've prefixed it with "really" (and 
emphasised that with several asterisks) - are you trying to tell me 
something? ;)

> I really want to interprete the
> last part as
>
>      foo = bar.default()

Yes, I can see that's a reasonable interpretation.


I never expected my suggestion to be embraced as-is, but perhaps it will 
inspire someone else to come up with a more enlightened suggestion - I 
did say that at the top of the post ;)

E.


More information about the Python-ideas mailing list