id( ) function question

Laszlo Nagy gandalf at
Thu Oct 15 18:12:59 CEST 2009

> None, True, False, integers and strings are not mutable. The only time 
> the id is the "same" between two objects is if they are the identical 
> two objects.
I'm aware of that. ;-)
> CPython just (as a performance optimization) re-uses the same objects 
> sometimes even if people think they're using different objects.
> In,
> >>> a = 2
> >>> b = 2
Integers are immutable so it was not part of my question.
> Christian's point is, I believe, that this is all an implementation 
> detail to the CPython platform and not a language-defined feature. 
> Other implementations may do other things, so one should not rely on 
> this behavior. Basically, don't use "is" unless you really know what 
> you're doing -- or are testing verses a singleton :) "is" is never the 
> right thing for numbers. Usually. Ahem.
Well, I now understand that this is also "theoretically" correct:

 >>> id({}) == id({})

The explanation is that you can only distinguish two objects if ARE 
objects, e.g. they both must exist. In the one-liner above, the dict 
objects are not directly compared, and they are not BOTH alive when 
their ids are taken, so this one liner is a meaningless expression. :-)

I'm always amazed how logical and clean Python is. I do not want to 
start flame here, but for other programming languages (like XXX :-) ) 
they could not even start a thread like this, because some terms and 
concepts in other languages are not defined precisely.


More information about the Python-list mailing list