is implemented with id ?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Sep 5 10:41:19 EDT 2012


On Wed, 05 Sep 2012 10:00:09 -0400, Dave Angel wrote:

> On 09/05/2012 09:19 AM, Franck Ditter wrote:
>> Thanks to all, but :
>> - I should have said that I work with Python 3. Does that matter ? -
>> May I reformulate the queston : "a is b" and "id(a) == id(b)"
>>   both mean : "a et b share the same physical address". Is that True ?
>> Thanks,
> 
> No, id() has nothing to do with physical address.  The Python language
> does not specify anything about physical addresses.  Some
> implementations may happen to use physical addresses, others arbitrary
> integers.  And they may reuse such integers, or not.  Up to the
> implementation.

True. In principle, some day there might be a version of Python that runs 
on some exotic quantum computer where the very concept of "physical 
address" is meaningless. Or some sort of peptide or DNA computer, where 
the calculations are performed via molecular interactions rather than by 
flipping bits in fixed memory locations.

But less exotically, Frank isn't entirely wrong. With current day 
computers, it is reasonable to say that any object has exactly one 
physical location at any time. In Jython, objects can move around; in 
CPython, they can't. But at any moment, any object has a specific 
location, and no other object can have that same location. Two objects 
cannot both be at the same memory address at the same time.

So, for current day computers at least, it is reasonable to say that 
"a is b" implies that a and b are the same object at a single location.

The second half of the question is more complex:

"id(a) == id(b)" *only* implies that a and b are the same object at the 
same location if they exist at the same time. If they don't exist at the 
same time, then you can't conclude anything.



-- 
Steven



More information about the Python-list mailing list