is operator versus id() function
Nobody
nobody at nowhere.com
Sat Apr 6 09:35:03 EDT 2013
On Fri, 05 Apr 2013 06:49:14 -0700, Candide Dandide wrote:
> So, could someone please explain what exactly the is operator returns ?
> The official doc says :
>
> The ‘is‘ operator compares the identity of two objects; the id()
> function returns an integer representing its identity (currently
> implemented as its address).
The docs are correct.
But an identity is only unique for the lifetime of the object, so
"x is y" and "id(x)==id(y)" are only equivalent if the lifetimes of
x and y overlap.
If the objects have disjoint lifetimes (i.e. one is created after the
other has been destroyed), then it's possible for id() to return the same
value for both objects, so id(x)==id(y) can return a "false positive"
result, as happened in your example.
More information about the Python-list
mailing list