[Tutor] is vs. ==

Lloyd Hugh Allen lha2@columbia.edu
Fri, 19 Apr 2002 05:25:51 -0400


"Ian!" wrote:
> 
> What's the difference between "is" and "=="? I always assumed they were the
> same.
> 
> >>> __name__ == '__main__'
> 1
> >>> __name__ is '__main__'
> 0
> >>>
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

One last thing that I don't think has been addressed yet--one item "is"
another iff they have the same id #. Two integers less than 100, or two
strings (probably subject to some restriction), are likely to have the
same identity and so "is" each other. But for an example of two items
that are distinct but of equal value,

>>> thing1 = ['c','a','t']
>>> thing2 = ['c','a','t']
>>> id(thing1)
11119696
>>> id(thing2)
11121936
>>> thing1 == thing2
1
>>> thing1 is thing2
0

however, if you

>>> thing2 = thing1

then

>>> thing2 is thing1
1

and using list methods to modify will end up also modifying the other
(since they are the same object, with the same identity).