On 1/24/2020 3:36 AM, Victor Stinner wrote:
Le ven. 24 janv. 2020 à 03:37, Guido van Rossum <gu...@python.org> a écrit :
I think this started with a valuable optimization for `x in <list>`. I don't
know if that was ever carefully documented, but I remember that it was discussed a
few times (and IIRC Raymond was adamant that this should be so optimized -- which is
reasonable).
I'm tempted to declare this implementation-defined behavior -- *implicit* calls
to __eq__ and __ne__ *may* be skipped if both sides are the same object
depending on the whim of the implementation.
I think you are looking at too low a level. The container behavior
being discussed, of assuming that "identical objects are equal to
themselves"*, can be described in Python as testing equality of x and y
with "(x is y) or (x == y)"
* https://docs.python.org/3/reference/expressions.html#comparisons
CPython current behavior rely on the fact that it's possible to get
the memory address of an object.
No, this behavior relies on the language specification that all objects
have temporally unique integer ids that can be compared with 'is'.
I don't think that it should be part of Python language specification,
but seen as a CPython implementation detail.
Ids are a language feature; ids being addresses is a CPython detail, but
this detail is not relevant to equality comparison of items within
containers.
In PyPy, you may or may not have an "object". It depends how PyPy
decides to store values. For example, if a list only contains small
integers: PyPy uses a list specialized for integers and store
integers, not objects which stores integers.
Such lists are like strings, which also store small int values. Any
such container can pretend, in the current context, that id(n) is n so
that 'm is n' and 'm == n' are the same and only need to be computed once.
--
Terry Jan Reedy
_______________________________________________
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/XAJ554FPMXHEDDM5MTRYQGWIS7IZXWJC/
Code of Conduct: http://python.org/psf/codeofconduct/