Why this difference?

Paul Anton Letnes paul.anton.letnes at gmail.com
Thu Feb 24 13:58:28 CET 2011


Den 24.02.11 13.41, skrev n00m:
> The 1st "False" is not surprising for me.
> It's the 2nd "True" is a bit hmmm... ok, it doesn't matter
> ======================
> Have a nice day!

I am no expert, but I think python re-uses some integer and string 
objects. For instance, if you create the object int(2) it may be re-used 
later if you have several 2 objects in your code. This is to save some 
memory, or some other performance hack. Don't rely on it.

For instance:
 >>> a = 100
 >>> b = 100
 >>> a is b
True
 >>> a = 2**60
 >>> b = 2**60
 >>> a is b
False

Strange, but it's just like this!

Paul



More information about the Python-list mailing list