9

Possible Duplicate:
Python “is” operator behaves unexpectedly with integers

Ran into something odd last night where doing

if max_urls is 0:
    max_urls = 10

would always return false... even when max_urls was 0.... it was getting assigned from the database. When I did a

print type(max_urls)

would return

<type 'long'> 0

which seemed right but it would always return false.

If I changed it to

if max_urls == 0:
    max_urls = 10

then finally it would return true when it was 0. Why the difference between == and is?

Community
  • 1
  • 1
Wizzard
  • 12,582
  • 22
  • 68
  • 101
  • 3
    Do not use `is` to check numbers equality. BTW, Duplicate of http://stackoverflow.com/questions/306313/python-is-operator-behaves-unexpectedly-with-integers. – khachik Dec 17 '10 at 18:34
  • Another possible duplicate: http://stackoverflow.com/questions/3218308/types-for-which-is-keyword-may-be-equivalent-to-equality-operator-in-python – user225312 Dec 17 '10 at 18:35
  • Let's shorten this and skip to conclusions: Searching reveals a hundred duplicates, and all were asked by people who don't know the (very fundamental) difference between `==` and `is`. –  Dec 17 '10 at 18:39

2 Answers2

24

== is a value comparison, is is an object identity (memory location) comparison. You will often see that comparisons like max_urls is 0 will give the intended result because small values are usually cached in Python, but you always want to be using == instead of is when checking equality because this behavior cannot be relied upon.

Here is a brief example illustrating this:

>>> a = 0
>>> (a == 0, a is 0)
(True, True)
>>> a = 1000
>>> (a == 1000, a is 1000)
(True, False)
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • I'd stress it *doesn't* "work" as intended, though it does give the intended result. If you use the wrong means to get the right result, then not only do you have a potential bug that will come back to haunt you (e.g. later in maintenance), but you can't even test for it (e.g. current CPython makes no difference in this code, as you said). – Fred Nurk Dec 17 '10 at 18:51
6

The is operator checks that two references point to the same object. You are testing if long(0) is the same object as int(0), and the answer is no. This will be crystal clear if you print their object ids:

>>> max_urls = long(0)
>>> id(max_urls)
335952
>>> id(0)
8402324

== on the other hand checks that two values are equivalent, even if they are not the exact same object. For instance:

>>> a = 777
>>> b = 777
>>> a is b
False
>>> a == b
True
>>> id(a)
8404568
>>> id(b)
8404640

Note: It is important that I used 777 and not a smaller number like 1 or 2. Quoting from the Python manual:

The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.

Alessandro Cosentino
  • 2,268
  • 1
  • 21
  • 30
John Kugelman
  • 349,597
  • 67
  • 533
  • 578