is implemented with id ?

Dave Angel d at davea.name
Wed Sep 5 11:09:30 EDT 2012


On 09/05/2012 10:41 AM, Steven D'Aprano wrote:
> 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.

You're arguing against something i didn't say.  I only said that id()
doesn't promise to be a memory address.  i said nothing about what it
might mean if the "is" operator considers them the same.

> 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.
>
>
But by claiming that id() really means address, and that those addresses
might move during the lifetime of an object, then the fact that the id()
functions are not called simultaneously implies that one object might
move to where the other one used to be before the "move."

I don't claim to know the jython implementation.  But you're claiming
that id() means the address of the object, even in jython.  So if a
garbage collection can occur during the evaluation of the expression
       id(a) == id(b)

then the comparing of id()'s would be useless in jython.  Two distinct
objects could each be moved during evaluation, (very) coincidentally
causing the two to have the same addresses at the two times of
evaluation.  Or more likely, a single object could move to a new
location, rendering the comparison false.  Thus you have false positive
and false negative possible.

I think it much more likely that jython uses integer values for the id()
function, and not physical addresses.  I doubt they'd want a race condition.


-- 

DaveA




More information about the Python-list mailing list