tags:

views:

228

answers:

6

Is it better to use the "is" operator or the "==" operator to compare two numbers in Python?

Examples:

>>> a = 1
>>> a is 1
True
>>> a == 1
True
>>> a is 0
False
>>> a == 0
False
+3  A: 
>>> a = 255556
>>> a == 255556
True
>>> a is 255556
False

I think that should answer it ;-)

The reason is that some often-used objects, such as the booleans True and False, all 1-letter strings and short numbers are allocated once by the interpreter, and each variable containing that object refers to it. Other numbers and larger strings are allocated on demand. The 255556 for instance is allocated three times, every time a different object is created. And therefore, according to is, they are not the same.

Wim
The only safe use of `is` for comparisons is for the `None` object. And I suppose the `...` object.
Chris Lutz
`is` will work for all strings, not only 1-letter strings. This is referred to as string interning in the Python documentation.
sttwister
@ujukatzel - Wrong. When I run `a = "this is one hell of a string"; b = "this is one hell of a string"; a is b` I get `False` as the result. Python (specifically CPython) only interns some small strings.
Chris Lutz
@Chris Lutz - Although the example you posted does return `True` to me, indeed you're right, it does fail for some bigger strings.
sttwister
+3  A: 

That will only work for small numbers and I'm guessing it's also implementation-dependent. Python uses the same object instance for small numbers (iirc <256), but this changes for bigger numbers.

>>> a = 2104214124
>>> b = 2104214124
>>> a == b
True
>>> a is b
False

So you should always use == to compare numbers.

sttwister
+11  A: 

Use ==. Only integers from -1 to 256 will work with is.

Ignacio Vazquez-Abrams
Classes that define `__int__` won't work properly with `==` either; they'd need to define `__eq__` or `__cmp__` :)
Thomas Wouters
@Thomas: Fair enough. Fixed.
Ignacio Vazquez-Abrams
A: 

== is what you want, "is" just happens to work on your examples.

GregS
A: 
>>> 2 == 2.0
True
>>> 2 is 2.0
False

Use ==

Christopher Bruns
+6  A: 

Others have answered your question, but I'll go into a little bit more detail:

Python's is compares identity - it asks the question "is this one thing exactly identical to this other thing" (similar to === in JavaScript/PHP and == in Java). So, there are some times when using is makes sense - the most common one being checking for None. Eg, foo is None. But, in general, it isn't what you want.

==, on the other hand, asks the question "is this one thing logically equivalent to this other thing". For example:

>>> [1, 2, 3] == [1, 2, 3]
True
>>> [1, 2, 3] is [1, 2, 3]
False

And this is true because classes can define the method they use to test for equality:

>>> class AlwaysEqual(object):
...     def __eq__(self, other):
...         return True
...
>>> always_equal = AlwaysEqual()
>>> always_equal == 42
True
>>> always_equal == None
True

But they cannot define the method used for testing identity (ie, they can't override is).

David Wolever