❎Operadores de identidad
Los operadores de identidad se utilizan para comparar objetos. Sin embargo, no comparan si los objetos son iguales, en su lugar, comparan si son el mismo objeto:
Operador
Ejemplo
Significado
is
x is y
True
si las dos variables
son el mismo objeto
is not
x is not y
True
si las dos variables
no son el mismo objeto
batman = "Batman"
robin = "Robin"
batman is robin
False
help(id)
Help on built-in function id in module builtins:
id(obj, /)
Return the identity of an object.
This is guaranteed to be unique among simultaneously existing objects.
(CPython uses the object's memory address.)
# Tienen distintos IDs, por lo que son objetos diferentes
id(batman)
4370390448
id(robin)
4370389232
text1 = "Robin"
text2 = "Robin"
text1 == text2
True
text1 is text2
False
Última actualización