isExpert Examples

Identity operator; tests whether two variables reference the same object

is bytecode: IS_OP

How identity comparison compiles.

python
import dis

def check_is(x):
    return x is None

def check_is_not(x):
    return x is not None

def check_eq(x):
    return x == None

print("x is None:")
dis.dis(check_is)
print("\nx is not None:")
dis.dis(check_is_not)
print("\nx == None:")
dis.dis(check_eq)

# 'is' compiles to IS_OP (fast pointer comparison)
# '==' compiles to COMPARE_OP (calls __eq__)
# IS_OP has two forms: IS_OP 0 (is) and IS_OP 1 (is not)

'is' compiles to IS_OP which is a simple pointer comparison (O(1)). '==' compiles to COMPARE_OP which calls __eq__ and may be arbitrarily slow. 'is not' is a single opcode, not 'not' applied to 'is'.

Want to try these examples interactively?

Open Expert Playground