'==' vs. 'is' behavior

Fred L. Drake, Jr. fdrake at acm.org
Fri Nov 12 10:15:16 EST 1999


Andy Fox writes:
 > 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.
...
 > 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

  It's working just fine!  Note that the "small" integers (through 99) 
are always reused, so the "is" relationship will hold.  It's not very
useful, but reusing the integer objects saves a lot of allocation of
small numbers, and makes real-world code a lot faster.


  -Fred

--
Fred L. Drake, Jr.	     <fdrake at acm.org>
Corporation for National Research Initiatives




More information about the Python-list mailing list