>>> import ast>>> ast.dump(ast.parse('x is not None').body[0].value)"Compare(left=Name(id='x', ctx=Load()), ops=[IsNot()], comparators=[Name(id='None', ctx=Load())])">>> ast.dump(ast.parse('not x is None').body[0].value)"UnaryOp(op=Not(), operand=Compare(left=Name(id='x', ctx=Load()), ops=[Is()], comparators=[Name(id='None', ctx=Load())]))"
但是字节编译器实际上会将not ... is转换为is not:
>>> import dis>>> dis.dis(lambda x, y: x is not y)1 0 LOAD_FAST 0 (x)3 LOAD_FAST 1 (y)6 COMPARE_OP 9 (is not)9 RETURN_VALUE>>> dis.dis(lambda x, y: not x is y)1 0 LOAD_FAST 0 (x)3 LOAD_FAST 1 (y)6 COMPARE_OP 9 (is not)9 RETURN_VALUE