[Python-ideas] PEP 505: None-aware operators
Chris Angelico
rosuav at gmail.com
Thu Jul 19 14:51:47 EDT 2018
On Fri, Jul 20, 2018 at 4:45 AM, Stephan Houben <stephanh42 at gmail.com> wrote:
> Let me just address this point:
>
> 2018-07-19 20:36 GMT+02:00 Brendan Barnwell <brenbarn at brenbarn.net>:
>>
>> 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 @.)
>
>
> I think the actual reason is that it is a short-cutting operator, and none
> of
> the shortcutting operators (and, or, if/else) have an associated method.
> They cannot have, since they induce a non-standard evaluation order,
> hence their effect cannot be emulated with a method invocation.
Also for the same reason that the 'is' operator doesn't have a
corresponding dunder. You can't ask an object if it's the same object
as another; it either is or is not, intrinsically. It's the same here;
it either is None, or is not None, intrinsically. These new operators
have very clearly defined semantics involving the special object None,
and they short-circuit; two good reasons NOT to have them overridable.
ChrisA
More information about the Python-ideas
mailing list