[Tutor] fish or fowl?

Remco Gerlich scarblac@pino.selwerd.nl
Thu, 3 Jan 2002 13:57:56 +0100


On  0, Pijus Virketis <virketis@fas.harvard.edu> wrote:
> Dear all, 
> 
> here is a little thing I have been wondering for a while. In Python, I can say something like this:
> 
> >>> if raw_input() == "f": print "foo"
> 
> I get a prompt, and upon entering "f", foo is printed. Very nice, very
> convenient. Now, here's the question: how does the raw_input() call actually
> get evaluated? Is there a variable assignment going on in the background? As
> if I had typed:
>
> 
> >>> input_string = raw_input()
> >>> if input_string == "f": print "foo"
>
> Or is there some other behaviour? Can I retrieve the value afterwards? I
> guess I am just curious what happens under the hood here.

If you want to know, you can disassemble code objects.

Put it in a function:

def f():
   if raw_input() == "f": print "foo"
   
And then:

>>> import dis
>>> dis.disassemble(f.func_code)
          0 SET_LINENO               1

          3 SET_LINENO               2
          6 LOAD_GLOBAL              0 (raw_input)
          9 CALL_FUNCTION            0
         12 LOAD_CONST               1 ('f')
         15 COMPARE_OP               2 (==)
         18 JUMP_IF_FALSE           12 (to 33)
         21 POP_TOP             
			   
         22 SET_LINENO               2
         25 LOAD_CONST               2 ('foo')
         28 PRINT_ITEM          
         29 PRINT_NEWLINE       
         30 JUMP_FORWARD             1 (to 34)
    >>   33 POP_TOP             
    >>   34 LOAD_CONST               0 (None)
         37 RETURN_VALUE        

I don't know much about this either, but it's something like this:

- Set some line number info at the start of a function, for debugging
- Load a global 'raw_input' into the interpreter's memory spot 0
- Call the function in memory spot 0
- Load the constant 'f' into memory spot 1
- Compare the two
- If false, then goto the end of the block
- Else, load the constant 'foo' into memory spot 2
- Print it
- Print a newline
- Jump to the end
- Load None into memory spot 0
- Return that

(all functions return 0 if there was no explicit return statement)

I don't know what the 'pop top' lines are for, probably restore some memory
state to the way it was before.

No, you can't get to those memory spots from inside Python, afaik, although
you could construct a block of marshalled function code, de-marshall it,
transplant it into a function. Let the code put one of those values in a
variable. This is wizardry that is above my level though :-).


In Python, all you need to know is that 'raw_input() == "f"' is an
expression that is evaluated, first the left half ('raw_input()') is
evaluated by running the function, then the right half is evaluated ('"f"',
happens to be a constant so no further evaluation needed). Then they're
compared and an action is taken based on the whole result.

Or, in short, it just does what it seems to do, and you don't have to care
about the rest :)

-- 
Remco Gerlich