[Python-Dev] Need discussion for a PR about memory and objects
Steven D'Aprano
steve at pearwood.info
Mon Nov 19 22:07:27 EST 2018
Responding to a few points out of order.
On Mon, Nov 19, 2018 at 04:14:03PM -0800, Chris Barker via Python-Dev wrote:
> I think being a bit more explicit about what properties an ID has, and how
> the id() function works, and we may not need an anlogy at all, it's not
> that difficult a concept.
[...]
> I suggest something like the following:
>
> """
> Every object has an identity, a type and a value. An object’s identity
> uniquely identifies the object. It will remain the same as long as that
> object exists. No two different objects will have the same id at the same
> time, but the same id may be re-used for future objects once one has been
> deleted. The ‘is’ operator compares the identity of two objects; the id()
> function returns an integer representing its identity. ``id(object_a) ==
> id(object_b)`` if and only if they are the same object.
That looks good to me. However...
> And methions that in c_python the id is
> (currently) the memory address is a good idea for those that will wonder
> about it, and if there is enough explanation, folks that don't know about
> memory addresses will not get confused.
I don't think that the problem is that people don't understand the
"memory address as ID" implementation. I think the problem is people who
can't separate the implementation from the interface. jThere is a small
minority of developers, not just beginners, who insist that
(paraphrasing) "the id() function returns the object's memory address",
which leads others asking how to dereference the ID to get access to the
object.
E.g. I recently saw somebody on Reddit asking how to delete an object
given its address in Python.
I admit that this is just a minor point of confusion. We're not exactly
innundated with dozens of requests for pointer arithmetic and PEEK/POKE
commands *wink* but if we can reduce the confusion even further, that
would be nice. We've had 20+ years of telling people that the C memory
address of the object is an implementation detail, and some folks still
don't get it.
I'd like to that we reduce the emphasis on memory address in the docs.
Perhaps all the way to zero :-)
As you quoted, we currently we have a note in the docs that says:
**CPython implementation detail:** For CPython, id(x) is the memory
address where x is stored.
I'd like to banish that note to the C-API docs (I'm not sure where), the
FAQs (which apparently nobody ever reads *wink*) or perhaps just link to
the source and let those who care read if for themselves.
Instead, I'd like a more comprehensive comment directly in the
description of id, something like:
There are no guarantees made for the ID number except as above.
For example, Python implementations are known to take IDs from a
sequential series of integers (1, 2, 3, ...), or use arbitrary
implementation-defined values (263459012). Any such integer is
permitted so long as the ID is constant and unique for the
lifespan of the object.
--
Steve
More information about the Python-Dev
mailing list