DEV Community

Discussion on: What is the difference between ‘is’ and ‘==’ in python

Collapse
 
dwd profile image
Dave Cridland

The typical usages are if you do actually want to know if something is the same instance.

foo = True
if foo is True:  # True!

Also foo is None, which you've probably seen a lot of before, and useful with some constants if you're using them mostly by name, not value, like an enum type in other languages:

class enum:
  LEFT = 'Left'
  RIGHT = 'Right'

foo = enum.LEFT

print(foo is enum.LEFT)  # True! (And doesn't do a string comparison).
Collapse
 
nomade55 profile image
Lucas G. Terracino

Ok now is pretty clear, so at one poin if you change the value of foo to enum.RIGHT, this would be easier than checking if foo == "Right".

Thanks for the heads up!