On Fri, Jan 01, 2021 at 02:53:33PM -0500, Eric V. Smith wrote:
> On 1/1/2021 2:00 PM, Jonathan Fine wrote:
> >
> >By the way, this surprised me. Would anyone like to explain this?
> >    >>> id(f()), id(f())
> >    (139927013695536, 139927013695536)
> >
> id's are only unique while an object exists. Here the object returned by 
> the first call is discarded after its id is taken. Then the second 
> object is created and reuses the memory address that was used by the 
> first object. Since in CPython id() returns object's memory address, the 
> id's are the same.

What Eric said, except I would put more emphasis on two things:

1. I would emphasize more strongly that IDs are abstract identity 
numbers, not memory addresses except as an accident of implementation. 
For example in Jython and IronPython, the IDs are sequential numbers 
and won't be reused.

In general, if the Python implementation is using a compacting garbage 
collector where objects can move in memory, IDs cannot be memory 
addresses.


2. And that it's a matter of happenstance, not design, that the same 
memory address happens to be used.

I can't produce a demonstration right now, but I've seen senarios where 
every *second* temporary object reuses the previous IDs, so printing a 
sequence of IDs goes something like this:

    1012, 1042, 1012, 1042, 1012, 1042 ...

Presumably that could happen if the garbage collector takes longer to 
reclaim each temporary object than it takes the interpreter to generate 
and process the next one.



-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/PALNX6J4BLGXTHPH5NPOS3UTFOETU3HK/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to