python 2 coercion

Terry Reedy tjreedy at udel.edu
Tue Aug 31 16:57:10 EDT 2004


>What happens (on the C level) if the python interpreter executes
>a statement like
>if <some strange extension type> == None:

For Jython, there is no C level ;-)  For CPython, the beginning point for 
such a question is the byte code (this is 2.2, but should be same in 2.3, 
but you can check):

>>> def equalNone(a): return a == None
...
>>> import dis
>>> dis.dis(equalNone)
          0 SET_LINENO               1

          3 SET_LINENO               1
          6 LOAD_FAST                0 (a)
          9 LOAD_GLOBAL              1 (None)
         12 COMPARE_OP               2 (==)
         15 RETURN_VALUE
         16 LOAD_CONST               0 (None)
         19 RETURN_VALUE

Now go to ceval.c, find the COMPARE_OP branch in the master switch 
statement, and see what gets called.  Note that at this point, the 
interpreter has no idea what is bound to 'a'.

Terry J. Reedy






More information about the Python-list mailing list