access address from object and vice versa

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jan 22 00:52:00 EST 2012


On Sat, 21 Jan 2012 19:36:32 -0800, Chris Rebert wrote:

> On Sat, Jan 21, 2012 at 7:04 PM, Tamer Higazi <th982a at googlemail.com>
> wrote:
>> Hi people!
>> I have asked myself the following thing.
>>
>> How do I access the address of an object
> 
> id(obj) happens to do that in CPython, but it's a mere implementation
> detail.

I really wish that CPython didn't expose the fact that id happens to use 
address. That simply gives people the wrong idea.

Jython has the right approach, in my opinion. Objects are given IDs on 
request, starting with 1, and no id is ever re-used:

steve at runes:~$ jython
Jython 2.5.1+ (Release_2_5_1, Aug 4 2010, 07:18:19)
[OpenJDK Client VM (Sun Microsystems Inc.)] on java1.6.0_18
Type "help", "copyright", "credits" or "license" for more information.
>>> id(None)
1
>>> id(True)
2

On the other hand, presumably this means that Jython objects need an 
extra field to store the ID, so the CPython approach is a space 
optimization.



-- 
Steven



More information about the Python-list mailing list