Unravelling `is` and `is not`
As part of this blog series on Python's syntactic sugar, I said in the post on unary arithmetic operators that it might be the most boring post in this series. I think I was wrong. 😄
The operators is
and is not
are very short. The documentation for the operators says the following:
x is y
is true if and only if x and y are the same object.  An Object’s identity is determined using theid()
function. Âx is not y
yields the inverse truth value.
That's it: call id()
on the objects in question and see if you got the same/different values for each of the objects.
The bytecode
Disassembling the two operators leads to the following:
If you follow into COMPARE_OP
you will see it calls cmp_outcome()
. The pertinent lines of that function are:
What that is showing is that is
and is not
are doing nothing more than comparing the pointers of the objects in question (which are the location in memory for the objects if you are unfamiliar with what a pointer is; PyPy returns a unique number instead).
The code
That translates is
and is not
to:
This is one of those instances where the tests took up more code than the implementation. 😄
As usual, the source code from this blog post can be found in my desugar project.