'==' vs. 'is' behavior

Andy Fox foxes at theriver.com
Fri Nov 12 10:04:10 EST 1999


Hi,

I thought I understood the difference between the '==' and 'is'
operators, but just found an exception to the rule.  Couldn't find an
explanation in the FAQs.

I understand the following:
>>> a = (1, 2)
>>> b = (1, 2)
>>> a == b #are they equivalent?  yes.
1
>>> a is b #are they the same object? no.
0

Why doesn't it work for a certain integers:
>>> a = 2
>>> b = 2
>>> a == b #are they equivalent?  yes.
1
>>> a is b #are they the same object? yes.  Why?
1

Further experimentation shows that this seems to work for integers
through 99, but that starting with 100 the results of the 'is' test
return 0, as I would expect.

sys.version yields
"Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32"

-- 
Andy Fox




More information about the Python-list mailing list