7

I often see var is None in Python code, using is to test equality only works for a very few objects like None. I used to think that it's better practice to use == to compare equality even if is is possible and to use is only if you want to test for the same identity.

However it seems to me like the majority of programmers prefer var is None over var == None. Is my impression wrong or is there a good reason why people prefer is?

  • 1
    By using == instead of is, someone could override the __eq__ method to always return True. If you want to see if var is actually the None singleton, then it is better to do var is None. See also What is the difference between “ is None ” and “ ==None ” and Python None comparison: should I use “is” or ==? – Vincent Savard Jun 10 '16 at 14:05
  • @VincentSavard Most of the times that doesn't matter, you have a function that is guaranteed to return None or an integer for example. – timakro Jun 10 '16 at 14:09
  • 2
    It doesn't matter until it does, and then it is hard to debug. It's good to show clearly what your intent is, and this is what the is operator does. – Vincent Savard Jun 10 '16 at 14:10
  • @VincentSavard You could write an answer, otherwise I'm goint to write it later. That you should use is to make clear that your intent is to check for exactly None answered my question. – timakro Jun 10 '16 at 14:21
12

The authoritative reason is because PEP-8 says so:

Comparisons to singletons like None should always be done with is or is not, never the equality operators.

Thus, if you ever find yourself in dire need of comparing exactly to True or False, you should also use is. Remember: in cases like these, 'truthiness' is generally preferred over comparison to an actual value. That is,

if somevar:
    ...

over

if somevar is True:
    ...

or

if somevar == True:
    ...

The main advantages I see are emphasizing the explicit desire to compare identity to a builtin, and the inability to break the comparison by defining __eq__ on arbitrary objects.


As a side note, you said:

I used to think that it's better practice to use == to compare equality even if is is possible and to use is only if you want to test for the same identity.

That's not merely 'better practice'. Equality vs. identity is the difference between those language constructs, and they should absolutely be used that way.

0

actually, it isn't about programming style, is and == are very different:

the is operator checks if the two items reference the same object

the == operator checks if the objects that the two items reference are the identicle

for example:

l = [1,2,3,4,5,6,7,8,9,1,2,3,4,]
l2 = l         # make ir reference the object that l references
l3 = l[:]      # copy it so reference identicle objects

l is l2 # same objects
True

l == l2 # identical objects
True

l is l3 # same objects
False

l == l3 # identical object
True

when you slice the list, you're copying it and creating another object identical to it; when you say l2 = l you're making a new variable that references the same object

source: Learning Python 5th edition by Mark Lutz; part II: Types and Operations, chapter 6: The Dynamic Typing Interlude pp. 180-184

  • 3
    The question is about the specific case where you compare with None. – CodesInChaos Jul 13 '16 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.