[Python-Dev] Re: print "%X" % id(object()) not so nice
Terry Reedy
tjreedy at udel.edu
Sat Nov 20 01:38:21 CET 2004
"James Y Knight" <foom at fuhm.net> wrote in message
news:582E4A36-3A6C-11D9-AA57-000A95A50FB2 at fuhm.net...
>[snip]
>I propose that id() always return a positive value ...
> Comments?
1. CPython intentionally searches builtins afters globals and pre-imports
the former as __builtins__ just so one can wrap a builtin to modify its
apparent behavior for personal needs.
>>> def id(o, max = 2**32):
... i = __builtins__.id(o)
... return (i < 0) and (max - i) or i
...
>>> id(a)
9158224
# only semi-tested because I don't have negatives to test and can't force
2. Given that, why bother changing the language for what must be an
esoteric need (to formattedly print and view ids)?
The id of an object is constant and unique with respect to contemporaneous
objects but, for CPython, definitely not with respect to objects with
non-overlapping lifetimes. (Newbies often get tripped by the last fact.).
>From the viewpoint of Python, ids have no meaning and are only useful for
identity comparision. For this purpose, arbitrary strings would have
worked as well as integers.
For convenience, CPython uses the apparent address stored as an int. But
this is strictly an implementation detail. On modern systems, that
'address' is, I believe, a process-specific virtual address which the
hardware memory management system maps the hidden real address -- which is
the only reason why systems with less than 2**31 memory can have addresses
at or above 2**31 to become negative ints.
Terry J. Reedy
More information about the Python-Dev
mailing list