is implemented with id ?

Dave Angel d at davea.name
Wed Sep 5 10:00:09 EDT 2012


Please don't top-post.  Now your message is out of order, and if I have
to delete the part Benjamin said.

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.

And as others have pointed out, when you compare two id's, you're
risking that one of them may no longer be valid.  For example, the
following expression:

     flag = id(func1()) == id(func2())

could very well evaluate to True, even if func1() always returns a
string, and func2() always returns an int.  On the other hand, the 'is'
expression makes sure the two expressions are bound to the same object.

If a and b are simple names, and not placeholders for arbitrary
expressions, then I  THINK  the following would be true:

"a is b"  and   "id(a) == id(b)"  both mean that the names a and b are
bound to the same object at the time the statement is executed.

-- 

DaveA




More information about the Python-list mailing list