On Fri, Jan 24, 2020 at 4:47 AM Steven D'Aprano <st...@pearwood.info> wrote:

> On Fri, Jan 24, 2020 at 10:57:11PM +1100, Chris Angelico wrote:
> > On Fri, Jan 24, 2020 at 10:44 PM Steven D'Aprano <st...@pearwood.info>
> wrote:
> > >
> > > The only thing I'm unsure of here is whether direct use of the `==` and
> > > `!=` operators are included as "implicit calls" to the dunders. I
> > > *think* I understand Guido's intention, but I'm not sure:
> > >
> > > * x == y MUST call `__eq__`
> > >
> > > * likewise x != y MUST call `__ne__`
>
> Oh, that's too strong. The interpreter ought to be allowed to bypass the
> method call if it is knows that reflexivity certainly holds.
>

In this context the intention was just to clarify that the shortcut using
(x is y) is not allowed. If the types are known to the compiler or JIT it
can of course optimize what it wants, but if there is for example a side
effect in the implementation of __eq__ or __ne__ this must be visible.


> Obviously it can't know that in the general case of two arbitrary
> objects, but if the interpreter knows that both objects are a built-in
> type (not a subclass) apart from float, then it *might* choose to test
> for identity.
>
> In other words, atomic objects MUST NOT *assume* reflexivity[1] of
> arbitrary types, but MAY take advantage of reflexivity of specific known
> types.
>
> (E.g. ints don't have NANs, so there's no harm in allowing the
> interpreter to skip the method call if it knows both operands are actual
> built-in ints.)
>

All this is always understood -- the interpreter can optimize things away
if there's no way for the user to tell (apart from measuring timings or
inspecting generated code).

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/QXDD47DHUE72HS4POOLHVQTJ4N6WV7VC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to