On Fri, Jan 24, 2020 at 05:45:35AM -0500, Terry Reedy wrote:
> On 1/24/2020 3:36 AM, Victor Stinner wrote:

> >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.

Thanks Terry for raising this. Of course CPython can optimize identity 
tests by checking for the same memory address, and *memory address* is 
an implementation-detail of object identity.

Any Python implementation ought to have a sense of object identity, 
however it is implemented or emulated. For instance, both Java and .Net 
have compacting memory models, which means objects can move around in 
memory and there's no well-defined "memory address", but both have to be 
able to implement the `is` operator:

* Java has the `==` operator;
* .Net, or at least C#, has Object.ReferenceEquals

If there happens to be a Python interpreter where testing object 
identity is expensive, then they should be free to *not* bypass the 
equality test.


Victor:
> >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.

I believe that PyPy nevertheless offers the illusion that such lists 
contain objects, complete with IDs. They even manage to keep the ID 
consistent across copies. For example:

    >>>> L = [99]
    >>>> id(L[0])
    1585
    >>>> id(L[:][0])
    1585

so even if the list is implemented as an array of native integers rather 
than int objects, as far as Python code is concerned there is no 
behavioural difference.


-- 
Steven
_______________________________________________
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/LJKV6EBDNKL5MYDWNSDVWBR26MEUJF2R/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to