I am not asking when to use ==
vs is
in Python which is a question that has been asked and answered many times before.
As stated here, ==
checks if two arguments have the same value, and is
checks if the they refer to the same object.
So if one creates two objects with the same value,
>>> fruit_name = 'Apple'
>>> company_name = 'Apple'
one would expect ==
to return True
and is
to return False
. However this is what actually happens:
>>> fruit_name == company_name # True expected since they have the same value
True
>>> fruit_name is company_name
True
Why is Python considering fruit_name
and company_name
to be the same object as opposed to different objects with the same value? And when can one rely on is
to return True if and only if the arguments are intentionally set to be the same object by the programmer?