[Python-ideas] PEP 505: None-aware operators
Brendan Barnwell
brenbarn at brenbarn.net
Thu Jul 19 14:48:16 EDT 2018
On 2018-07-19 11:43, Elazar wrote:
>
> On Thu, Jul 19, 2018 at 11:37 AM Brendan Barnwell <brenbarn at brenbarn.net
> <mailto:brenbarn at brenbarn.net>> wrote:
>
>
> 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 @.)
>
>
> It is just as specific as the `is` operator, and for the same reason.
What reason is that?
The "is" operator is the ONLY operator that is un-overridable in this
way, and that's because by its nature it is concerned with object
identity, which we don't want people to be able to redefine. (Okay, the
logical operators like "and" and "or" are also un-overridable, but
that's not because we don't want people to be able to override them,
it's because it's hard to figure out a way to do it practically without
giving up short-circuiting.)
From my perspective "A if A is None else B" is just not remotely close
to being as special as "A is B". If we didn't have the "is" operator
there would be no way to do what it does. Everything that these new
operators do can already be done.
--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no
path, and leave a trail."
--author unknown
More information about the Python-ideas
mailing list