<p>Is there any difference between:</p>

<pre><code>if foo is None: pass
</code></pre>

<p>and</p>

<pre><code>if foo == None: pass
</code></pre>

<p>The convention that I've seen in most Python code (and the code I myself write) is the former, but I recently came across code which uses the latter.  None is an instance (and the only instance, IIRC) of NoneType, so it shouldn't matter, right?  Are there any circumstances in which it might?</p>

[{'answer_id': 26611, 'author': 'Brendan', 'author_id': 199, 'author_profile': 'https://Stackoverflow.com/users/199', 'pm_score': 9, 'selected': True, 'text': '<p><code>is</code> always returns <code>True</code> if it compares the same object instance</p>\n\n<p>Whereas <code>==</code> is ultimately determined by the <code>__eq__()</code> method</p>\n\n<p>i.e.</p>\n\n<pre><code>\n>>> class Foo(object):\n       def __eq__(self, other):\n           return True\n\n>>> f = Foo()\n>>> f == None\nTrue\n>>> f is None\nFalse\n</code></pre>\n'}, {'answer_id': 26626, 'author': 'borrego', 'author_id': 2772, 'author_profile': 'https://Stackoverflow.com/users/2772', 'pm_score': 6, 'selected': False, 'text': '<p>You may want to read this <a href="http://mail.python.org/pipermail/python-list/2001-November/094920.html" rel="noreferrer">object identity and equivalence</a>.</p>\n\n<p>The statement \'is\' is used for object identity, it checks if objects refer to the same instance (same address in memory).</p>\n\n<p>And the \'==\' statement refers to equality (same value).</p>\n'}, {'answer_id': 26654, 'author': 'Stephen Pellicer', 'author_id': 360, 'author_profile': 'https://Stackoverflow.com/users/360', 'pm_score': 3, 'selected': False, 'text': '<p>For None there shouldn\'t be a difference between equality (==) and identity (is). The NoneType probably returns identity for equality. Since None is the only instance you can make of NoneType (I think this is true), the two operations are the same. In the case of other types this is not always the case. For example:</p>\n\n<pre><code>list1 = [1, 2, 3]\nlist2 = [1, 2, 3]\nif list1==list2: print "Equal"\nif list1 is list2: print "Same"\n</code></pre>\n\n<p>This would print "Equal" since lists have a comparison operation that is not the default returning of identity.</p>\n'}, {'answer_id': 26963, 'author': 'Graeme Perrow', 'author_id': 1821, 'author_profile': 'https://Stackoverflow.com/users/1821', 'pm_score': 3, 'selected': False, 'text': '<p>@<a href="https://stackoverflow.com/questions/26595/is-there-any-difference-between-foo-is-none-and-foo-none#26698">Jason</a>:</p>\n\n<blockquote>\n  <p>I recommend using something more along the lines of</p>\n\n<pre><code>if foo:\n    #foo isn\'t None\nelse:\n    #foo is None\n</code></pre>\n</blockquote>\n\n<p>I don\'t like using "if foo:" unless foo truly represents a boolean value (i.e. 0 or 1). If foo is a string or an object or something else, "if foo:" may work, but it looks like a lazy shortcut to me. If you\'re checking to see if x is None, say "if x is None:".</p>\n'}, {'answer_id': 28067, 'author': 'Tendayi Mawushe', 'author_id': 2979, 'author_profile': 'https://Stackoverflow.com/users/2979', 'pm_score': 5, 'selected': False, 'text': '<p>A word of caution: </p>\n\n<pre><code>if foo:\n  # do something\n</code></pre>\n\n<p>Is <strong>not</strong> exactly the same as:</p>\n\n<pre><code>if x is not None:\n  # do something\n</code></pre>\n\n<p>The former is a boolean value test and can evaluate to false in different contexts. There are a number of things that represent false in a boolean value tests for example empty containers, boolean values. None also evaluates to false in this situation but other things do too.</p>\n'}, {'answer_id': 585491, 'author': 'Mykola Kharechko', 'author_id': 69885, 'author_profile': 'https://Stackoverflow.com/users/69885', 'pm_score': 4, 'selected': False, 'text': '<p><code>(ob1 is ob2)</code> equal to <code>(id(ob1) == id(ob2))</code></p>\n'}, {'answer_id': 2932590, 'author': 'mthurlin', 'author_id': 39991, 'author_profile': 'https://Stackoverflow.com/users/39991', 'pm_score': 4, 'selected': False, 'text': '<p>The reason <code>foo is None</code> is the preferred way is that you might be handling an object that defines its own <code>__eq__</code>, and that defines the object to be equal to None. So, always use <code>foo is None</code> if you need to see if it is infact <code>None</code>.</p>\n'}, {'answer_id': 5451786, 'author': 'ncmathsadist', 'author_id': 467379, 'author_profile': 'https://Stackoverflow.com/users/467379', 'pm_score': 1, 'selected': False, 'text': "<p>John Machin's conclusion that <code>None</code> is a singleton is a conclusion bolstered by this code.</p>\n\n<pre><code>&gt;&gt;&gt; x = None\n&gt;&gt;&gt; y = None\n&gt;&gt;&gt; x == y\nTrue\n&gt;&gt;&gt; x is y\nTrue\n&gt;&gt;&gt; \n</code></pre>\n\n<p>Since <code>None</code> is a singleton, <code>x == None</code> and <code>x is None</code> would have the same result.  However, in my aesthetical opinion, <code>x == None</code> is best.</p>\n"}, {'answer_id': 16636124, 'author': 'Bleeding Fingers', 'author_id': 1309352, 'author_profile': 'https://Stackoverflow.com/users/1309352', 'pm_score': 2, 'selected': False, 'text': '<p>Some more details:</p>\n\n<ol>\n<li><p>The <code>is</code> clause actually checks if the two <code>object</code>s are at the same\nmemory location or not. i.e whether they both point to the same\nmemory location and have the same <code>id</code>. </p></li>\n<li><p>As a  consequence of 1, <code>is</code> ensures whether, or not, the two lexically represented <code>object</code>s have identical attributes (attributes-of-attributes...) or not</p></li>\n<li><p>Instantiation of primitive types like <code>bool</code>, <code>int</code>, <code>string</code>(with some exception), <code>NoneType</code> having a same value will always be in the same memory location.</p></li>\n</ol>\n\n<p>E.g.</p>\n\n<pre><code>&gt;&gt;&gt; int(1) is int(1)\nTrue\n&gt;&gt;&gt; str("abcd") is str("abcd")\nTrue\n&gt;&gt;&gt; bool(1) is bool(2)\nTrue\n&gt;&gt;&gt; bool(0) is bool(0)\nTrue\n&gt;&gt;&gt; bool(0)\nFalse\n&gt;&gt;&gt; bool(1)\nTrue\n</code></pre>\n\n<p>And since <code>NoneType</code> can only have one instance of itself in the python\'s "look-up" table therefore the former and the latter are more of a programming style of the developer who wrote the code(maybe for consistency) rather then having any subtle logical reason to choose one over the other.</p>\n'}, {'answer_id': 16636358, 'author': 'Thijs van Dien', 'author_id': 1163893, 'author_profile': 'https://Stackoverflow.com/users/1163893', 'pm_score': 3, 'selected': False, 'text': '<p>There is no difference because objects which are identical will of course be equal. However, <a href="http://www.python.org/dev/peps/pep-0008/" rel="noreferrer" title="PEP 8">PEP 8</a> clearly states you should use <code>is</code>:</p>\n\n<blockquote>\n  <p>Comparisons to singletons like None should always be done with is or is not, never the equality operators.</p>\n</blockquote>\n'}, {'answer_id': 27559554, 'author': 'Chillar Anand', 'author_id': 2698552, 'author_profile': 'https://Stackoverflow.com/users/2698552', 'pm_score': 3, 'selected': False, 'text': '<p><code>is</code> tests for identity, <strong>not</strong> equality. For your statement <code>foo is none</code>, Python simply compares the memory address of objects. It means you are asking the question "Do I have two names for the same object?"</p>\n\n<p><code>==</code> on the other hand tests for equality as determined by the <code>__eq__()</code> method. It doesn\'t cares about identity.</p>\n\n<pre><code>In [102]: x, y, z = 2, 2, 2.0\n\nIn [103]: id(x), id(y), id(z)\nOut[103]: (38641984, 38641984, 48420880)\n\nIn [104]: x is y\nOut[104]: True\n\nIn [105]: x == y\nOut[105]: True\n\nIn [106]: x is z\nOut[106]: False\n\nIn [107]: x == z\nOut[107]: True\n</code></pre>\n\n<p><code>None</code> is a singleton operator. So <code>None is None</code> is always true.</p>\n\n<pre><code>In [101]: None is None\nOut[101]: True\n</code></pre>\n'}, {'answer_id': 55489100, 'author': 'Aks', 'author_id': 4417090, 'author_profile': 'https://Stackoverflow.com/users/4417090', 'pm_score': 0, 'selected': False, 'text': '<pre><code>a is b # returns true if they a and b are true alias\na == b # returns true if they are true alias or they have values that are deemed equivalence \n\n\na = [1,3,4]\nb = a[:] #creating copy of list\na is b # if gives false\nFalse\na == b # gives true\nTrue\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/26595', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/156/']