1

I was learning cpython internals(especially the is operator) and came across the following opcode implementation for IS_OP in ceval.c

case TARGET(IS_OP): {
   PyObject *right = POP();
   PyObject *left = TOP();
   int res = (left == right)^oparg;
   PyObject *b = res ? Py_True : Py_False;
   ...
}

I know the first two statements pop the operands from the stack.

PyObject *right = POP();
PyObject *left = TOP();

But my question is related to the following statement.

int res = (left == right)^oparg;

In my understanding is operator in python compares object identity, In other teams it will check whether both the objects are pointing to the same object. So my question is, only the following code is sufficient to check the object identity?, Why the actual implementation again apply an exlcusive OR(^) with oparg?

int res = (left == right)
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46

1 Answers1

2

The IS_OP op code is used for both is and is not. The ^oparg flips the result of the comparison when oparg is 1 (for is not) and leaves it alone when it's 0 (for is), so the same bytecode can handle both cases branchlessly just by tweaking the oparg (in dis output, is is IS_OP 0, is not is IS_OP 1).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Ha yes, I just disassembled both `is` and `is not`. Both have the same opcode `IS_OP` and only difference in `oparg`. Thank you for the answer. – Abdul Niyas P M Dec 09 '20 at 13:44