PEP 824: None-coalescing operators

to me user_name ?? default_name or "Guest" looks wrong. user_name ?? (default_name or "Guest") looks ok, but still dubious. It raises the question: why can’t we use user_name ?? default_name ?? "Guest"? Why would we allow default_name to ever be ""?

There might exist a compelling scenario which should decide whether ?? or or should bind stronger, but this isn’t it.

3 Likes

I wouldn’t call this “breaking” so much as complicated logic that is difficult to express succinctly. I don’t think the ability to use ?? with a compatible precendence order actually helps comprehending what the author’s intent is. That’s what I generally mean as “readable”. Can I, without the help of comments, easily grok not what the code does but what the author’s intent is? Parentheses help, but don’t settle the matter to me.

I thought the same thing, but as you point out, there’s a potential gotcha.

This is all great stuff that I really want to see in the PEP! :winking_face_with_tongue:

Agreed, this example is unrealistic and not helpful as a yardstick for different options.

I’m not sure we’re all taking about the same thing at the moment.

  1. Is it that the precedence section is missing a clear rational?
  2. Or that the precedence for ?? should be changed to something else? And if so to what?

I agree that it’s not fully obvious in the PEP why the precedence should be exactly where it is. For context, the main decision I made while writing the PEP was that ?? should not be a binary operator like PEP 505 proposed it, but a boolean one instead. The rational for that is cleared stated in the rejected idea Add ?? as a binary operator section. As a boolean operator, I chose to assume the precedence from JS, C# and Dart. (Also mentioned at the end of the already mentioned rejected idea section.) There wasn’t any reason for me to consider something else honestly and I don’t think Python needs to reinvent the wheel here either.

To me the precedence makes sense like I tried to explain earlier, but it might very well just be convention.

I think we’re trying a way to make Barry happy with the PEP (without disappointing the author). That could either be an argument for “weaker than or” (possibly with a more appealing example), or a change in the spec that satisfies everyone.

I would personally be happy if the parser required parentheses whenever ?? and or appear next to each other – and for all I care also when ?? and and appear together. That would be the JS/TS approach, and given that other mainstream languages firmly disagree, it seems a wise choice.

I’ll defer to Barry to explain the process for updating the PEP after it’s been submitted to the SC.

2 Likes

Another procedural idea: ask the SC to pick between two proposals (e.g. current PEP and JS/TS).

The PEP was just submitted to the SC so we haven’t even officially started discussing it. There were some informal chats at the core sprint, but we still need to put it on our agenda (could happen at this Thursday’s regular meeting).

One piece of feedback I would give to the authors :wink: would be to include the rationale for why the specific precedence order the authors propose was chosen. I want to be able to say “the authors thought about the different options, settled on option X for the reasons given”.

I might have expressed a personal preference, but I’m quite possibly wrong! given the competing options and their rationales. So don’t make a change to appease me, just explain in the PEP why you made the deliberate choice you made. If given the discussion in the thread you think a different choice should be made, then it’s okay to change your mind.

If you can update the PEP in the next day or two, it’s probably fine.

2 Likes

Okay, we’ll shoot for that. Just to be sure, when the updated version has landed, we’ll ping the existing pep 824 issue in the SC’s tracker.

Thanks, sounds great!

PEP 824 has been updated with an extended rationale for the precedence choice (in the Rejected Ideas section).

3 Likes

FWIW, the precedence of or being greater than ?? is confusing for me. x ?? y or z, we evaluate if x is None, if it isn’t evaluate y or z. I’d think it pretty rare that we’d want to keep a Falsy but not None value for x, and so I see the or as a Falsy check over the whole thing. Might just be a consequence of not having to reason about another short circuiting operator.

Though we should use parens regardless, so this should be moot.