'==' vs. 'is' behavior
Fred L. Drake, Jr.
fdrake at acm.org
Fri Nov 12 16:15:16 CET 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.
> >>> a is b #are they the same object? yes. Why?
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 L. Drake, Jr. <fdrake at acm.org>
Corporation for National Research Initiatives
More information about the Python-list