3

the python keyword is is supposed to be used in place of the == operator according to python style guides.

However they don't always do exactly the same thing as shown here. Why? What is the actual difference, and what is the proper usage?

import unittest

class testIS(unittest.TestCase):
    def test_is(self):
        self.assertEqual(1,1)

if __name__ == '__main__':
    unittest.main()

Which works... but the following does not...

import unittest

class testIS(unittest.TestCase):
    def test_is(self):
        self.assertEqual(1,1)

if __name__ is '__main__':
    unittest.main()
warvariuc
  • 57,116
  • 41
  • 173
  • 227
jonathanbsyd
  • 8,031
  • 6
  • 24
  • 26
  • 1
    possible duplicate of [String comparison in Python: is vs. ==](http://stackoverflow.com/questions/2988017/string-comparison-in-python-is-vs) – Mat Mar 16 '12 at 07:11
  • possible duplicate of [Python '==' vs 'is' comparing strings, 'is' fails sometimes, why?](http://stackoverflow.com/questions/1504717/python-vs-is-comparing-strings-is-fails-sometimes-why) – Sudhir Jonathan Mar 16 '12 at 07:12

6 Answers6

7

== tests for equality. Two non-identical objects can be equal.

is tests for identity, i.e. whether both refer to the same one object.

hamstergene
  • 24,039
  • 5
  • 57
  • 72
  • Better is to say `==` tests for value while `is` tests for identity. – orlp Mar 16 '12 at 07:23
  • 1
    @nightcracker: no, the use of the ambiguous term *equality* is actually acurate, since the definition of equality is defined by the type of the objects. Unless you override `__eq__` in user-defined classes, `==` checks for identity, not value. – André Caron Mar 16 '12 at 07:34
  • @André Caron: what the default implementation is, is a guess, where identity happens to be a pretty good guess in Python. But it by no means changes it's semantic meaning, which is test for equality by value. – orlp Mar 16 '12 at 07:37
  • 1
    @nightcracker 'value' is a subtle term. I can define that objects of a type are equal with probability 0.5 regardless of their value, for example. Or I can have a temporary object 'ignorecase(str)' which can do nothing but be compared for equality with a string. Discussing what value do these objects have seems meaningless for me, yet testing for equality can make sense. – hamstergene Mar 16 '12 at 07:39
  • @nightcracker: The class can make `__eq__` mean whatever makes sense with respect to the abstraction, including equality by identity. This doesn't have to be equality by value. – André Caron Mar 16 '12 at 07:44
5

is will return True if two variables point to the same object, == if the objects referred to by the variables are equal.

>>> a = [1, 2, 3]
>>> b = a
>>> b is a 
True
>>> b == a
True
>>> b = a[:]
>>> b is a
False
>>> b == a
True
akhter wahab
  • 4,045
  • 1
  • 25
  • 47
1
if money_in_wallet is money_that_was_in_wallet(two_weeks_ago):
    print("I still live with my parents and have no income or expenses")
elif money_in_wallet == money_that_was_in_wallet(two_weeks_ago):
    print("Good, my budget is exactly balanced")
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
0

Check out...

http://docs.python.org/reference/expressions.html#not-in

... Which states...

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object

Martin Peck
  • 11,440
  • 2
  • 42
  • 69
0

The Python keyword 'is' checks for object identity, while == operator checks for equality of values. For example:

>>> if Car1 is Car2:
>>>     [do something...]

this code tests if Car1 and Car2 refer to the same car, while

>>> if Car1 == Car2:
>>>     [do something...]

checks if Car1 and Car2 are of same quality, that is if they are of the same model, color, etc.

For this reason, __name__ is "__main__" returns False, because the string "__main__" and the value stored in __name__ are really two different string objects. So to check if the value of __name__ string is equal to __main__, use == operator.

Nazmun
  • 1
0

is tests if both inputs are actually the same object. That is located the same address in memory.

== calls the __eq__ method on one of the input objects. Objects can then define their own __eq__ method and decide what is equal and what is not.

Esben Skov Pedersen
  • 4,437
  • 2
  • 32
  • 46