Revisiting PEP 505

Splitting a None-coalescing proposal from safe navigation feels like a good idea to me. ? is already de facto reserved for this conceptual area anyway, so accepting such a proposal would just make that official, and a dedicated shorthand for lhs if lhs is not None else rhs would help eliminate some dubious uses of lhs or rhs in the same way that conditional expressions themselves eliminated dubious uses of c and a or b.

Safe navigation, by contrast, suffers from a fundamental ambiguity problem in Python (specifically, whether it implicitly handles exceptions or not) that doesn’t exist in JavaScript. In C#, the null-propagating operators do not suppress exceptions, so it would be surprising to at least some users if Python made the opposite choice.

This essential ambiguity is the origin of the “query expression” concept in Safe navigation operators by way of expression result queries (thread previously linked near the start of this discussion).

Edit: I forgot to add that I’ve been doing a fair bit of unvalidated JSON processing lately, and dict pattern matching has been entirely up to the task in the handful of cases where I might otherwise have wished for safe navigation support.

21 Likes