
On 2018-07-19 02:11, Chris Angelico wrote:
-snip- As far as I can see, these null-coalescing operators would break that model. The PEP doesn't seem to provide for a "real" magic method allowing users to override the actual behavior of the method. (You can only override __has_value__ to hook into it, but not define by fiat what A ?? B does, as you can with other operators.) And I think the reason for this is that the operator itself is too specific, much more specific in semantics than other operators. (I had similar doubts about adding the matrix-multiplication operator @.)
People keep saying that this null-coalescing behavior is so common and useful, etc., but that hasn't been my experience at all. In my experience, the desire to shortcut this kind of logic is more often a sign of corner-cutting and insufficiently specified data formats, and is likely to cause bugs later on. Eventually it has to actually matter whether something is None or not, and these operators just kick that can down the road. In terms of their abstract meaning, they are not remotely close to as common or useful as operators like & and |.
Fully agree with you on this, so -1 on those new operators. I like operators (I was behind one of the early proposals for matmul operator (even proposed a bunch of others, for orthogonality purpose: element-wise/object-as-a-whole is a dichotomy that can happen for other things that multiplication), but they need to benefit from operators specifics: precendence order, tradition meaning direct translation of formula/recipies already written with a similar notation, and be generic enough to allow productive use of overloading (instead of confusing use). Matmul have this (with possible exception for overloading, but they are quite a few other mathematical entities where you want to have more than 1 multiplication, beside matrices). Those new operators, not so much, the only benefit is more compact notation of something that is already quite compact (A if A is not None else B) and very explicit (it reads like pseudocode, which is a huge strengh of python). ?? and associates is much more cryptic, force you to think much harder about precedence, just to be slightly more compact. Granted, compact notation can be valuable, but usually if you want to couple multiple such things (like you often do with matmul), and precedence rules allows you to do it elegantly combine multiple operator with infix notation (again matmul is present in formulas that have scalar mul, + and -) . Do we do this with None-coalescence? I don't think so, not often anyway... Do we want to do this more with None-coalescence? Not me, certainly not: When I None coalesce, I want it to be as clear and atomic as possible because it's a corner case. so strong -1...