Just when I though I knew enough about python operators!
Can someone explain why e is f false?
a = 'Goodbye'
b = 'Goodbye'
c = 'Good_Bye'
d = 'Good_Bye'
e = 'Good-Bye'
f = 'Good-Bye'
a is b
Out[9]: True
c is d
Out[10]: True
e is f
Out[11]: False
Just when I though I knew enough about python operators!
Can someone explain why e is f false?
a = 'Goodbye'
b = 'Goodbye'
c = 'Good_Bye'
d = 'Good_Bye'
e = 'Good-Bye'
f = 'Good-Bye'
a is b
Out[9]: True
c is d
Out[10]: True
e is f
Out[11]: False
is
checks for identity of two objects.
The ‘is’ operator compares the identity of two objects; the id() function returns an integer representing its identity.
For immutable (e.g. str
) literals, equal values can (incidentally) actual rely on the same object underpinning them, but that is not guaranteed or intentional -> you should not rely on that (emphasis added):
Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation.