Today I learnt about the is
keyword in Python and tried the following:
>>> x=2+2
>>> y=4
>>> x is y
True
I started off trying is
with integers because I knew the answer would be False
— so I found the result very surprising! To give some context, my background is C++ and C# where there is a distinction between value types and object types. In Python, as I now realize, everything is a reference type.
It seems the reason that x is y
is True
is the same as explained in this question, How is the 'is' keyword implemented in Python?, which relates to using is
with strings. I.e. the run-time environment saves memory by sharing, or "interning" integers just as it does with strings — this is explained in more detail in answers to a question: Python “is” operator behaves unexpectedly with integers I found after my initial post.
Another thing I find surprising is that the value that is
returns is implementation dependent. Which relates to my main question. In the referenced question about the implementation of is
w.r.t strings, there was some discussion about when to use is
, with several users saying they would (almost) never use it. So my question is, when should the is
keyword be used? What are some canonical examples, or general rules?
Best Answer
You should use
is
when you want to know whether two objects are the same object. Don't use it when you want to know whether two objects have the same value.There is a canonical example, but it is unfortunately not very helpful. People will tell you to always test for the
None
value usingx is None
instead ofx == None
. However, there is little practical difference between these cases. (See this question for explanation.)In some situations, you may wind up creating objects which have the same value but are distinct objects. For instance, you could imagine creating a fantasy wargame in which the player can magically create minions to battle his opponent. So the player might create 100 identical orcs or whatever. Each orc could be represented by an object, and they would be identical in that they have the same properties, but still distinct in that there would be 100 separate objects. Now if the opponent tries to cast a "fireball" spell on one of these orcs, while on the same turn the player tries to cast "protect against fire" on an orc, you might want to check if the target of the fireball spell
is
the target of the protection spell. Equality wouldn't be enough, because all the orcs are equal, but only one particular orc is the target of each spell, and you want to know if the two targets are the same object. This is a rather contrived example, but should give a broad idea of the kind of situation where you might wind up usingis
.