Object identity has no necessary connection to memory location (was: What is a function parameter =[] for?)

Chris Angelico rosuav at gmail.com
Thu Nov 26 06:49:14 EST 2015


On Thu, Nov 26, 2015 at 10:24 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
> Dave Farrance <df at see.replyto.invalid> writes:
>
>> Marko Rauhamaa <marko at pacujo.net> wrote:
>>
>> >Dave Farrance <df at see.replyto.invalid>:
>> >
>> >> (Conversely, I see that unlike CPython, all PyPy's numbers have
>> >> unchanging ids, even after exiting PyPy and restarting, so it seems
>> >> that PyPy's numerical ids are "faked".)
>> >
>> >What's a faked id?
>>
>> You can figure out what I'm getting at -- i.e. I presume that the ids
>> are not pointers to stored numbers in memory (as with CPython) but are
>> a translation of the numerical variable's value.
>
> Why refer to that as “faked”? That's what I can't figure out about what
> you're getting at. Perhaps Marko shares my uncomprehension.
>
> The Python language makes no promise about “pointers to stored numbers
> in memory” for object identity. That is an implementation detail of
> CPython, and is *explicitly* not promised for any other Python
> implementation.
>
> If you are surprised that object identity appears to have no connection
> with memory location, then you've made unwarranted assumptions that are
> explicitly warned against in the Python documentation.

I can't remember which language it was (maybe Lua?), but I know
there's one that uses a machine word to store either a pointer to a
heap object, or an integer of at most one less bit than the machine
word, represented by 2*n+1. A system like this would work for Python
object IDs; it guarantees that all heap objects have unique even IDs
(because no two can share the same address; all it takes is requiring
16-bit alignment, and most modern CPUs will give at least 32-bit), and
all small integers will be slotted in between them. So the integer
objects might themselves be entirely faked, never actually existing in
memory, yet never violating the rule of object IDs. (Any integer too
big to fit in a machine word minus one bit would be heap-allocated, so
they're safe.)

That's "faked", in a sense, but still perfectly compliant.

ChrisA



More information about the Python-list mailing list