Resumen:
Úselo iscuando desee verificar la identidad de un objeto (por ejemplo, verificar si vares así None). Úselo ==cuando desee verificar la igualdad (por ejemplo, es varigual a3 ?).
Explicación:
Puede tener clases personalizadas donde my_var == NonevolveráTrue
p.ej:
class Negator(object):
def __eq__(self,other):
return not other
thing = Negator()
print thing == None #True
print thing is None #False
iscomprueba la identidad del objeto . Solo hay 1 objeto None, así que cuando lo haces my_var is None, estás verificando si realmente son el mismo objeto (no solo equivalente objetos )
En otras palabras, ==es una verificación de equivalencia (que se define de un objeto a otro) mientras que isverifica la identidad del objeto:
lst = [1,2,3]
lst == lst[:] # This is True since the lists are "equivalent"
lst is lst[:] # This is False since they're actually different objects
is- python.org/dev/peps/pep-0008/#programming-recommendations