Surprise using the 'is' operator

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Sep 26 06:32:02 EDT 2006


"codefire" <tony.bedford at gmail.com> writes:

> I was just trying to check if objects were the same (object), didn't
> know Integers were a special case.

They're not. The Python runtime environment can do whatever it likes
underneath the hood; the language gives no promises about any
relationship between 'is' and '=='.

What you may be missing is that 'c = 10' is not "putting the value 10
into the box named c". It is rather "putting the label c onto the
object 10".

Whether there are one, two or twelve instances of an object '10' is
entirely up to the Python runtime implementation. You can use 'is' to
check whether two objects are identical; you can use '==' to check
whether they are equal in value; you cannot generalise anything about
the relationship between those two.

-- 
 \         "I still have my Christmas Tree. I looked at it today. Sure |
  `\            enough, I couldn't see any forests."  -- Steven Wright |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list