Printing of individually returned values when running interactively

Greg Ewing greg.ewing at compaq.com
Wed Nov 3 09:58:37 CET 1999


Hrvoje Niksic wrote:
> 
> I've just noticed (the hard way) a very unusual feature of Python's
> interactive interpreter mode.  If you evaluate several values on
> several lines, all the values are printed!
>
>  Why would anyone want a thing like that?

I doubt anyone ever explicitly wanted it. I suspect it's a
side effect of the implementation. I just did the following
experiment:

>>> c = compile("""
if 1:
	1
	2
	3
""", "", "single")
>>> c
<code object ? at 815b00, file "", line 0>
>>> from dis import dis
>>> dis(c)
          0 SET_LINENO          0

          3 SET_LINENO          2
          6 LOAD_CONST          0 (1)
          9 JUMP_IF_FALSE      25 (to 37)
         12 POP_TOP        

         13 SET_LINENO          3
         16 LOAD_CONST          0 (1)
         19 PRINT_EXPR     

         20 SET_LINENO          4
         23 LOAD_CONST          1 (2)
         26 PRINT_EXPR     

         27 SET_LINENO          5
         30 LOAD_CONST          2 (3)
         33 PRINT_EXPR     
         34 JUMP_FORWARD        1 (to 38)
    >>   37 POP_TOP        
    >>   38 LOAD_CONST          3 (None)
         41 RETURN_VALUE   

In other words, in interactive mode, the compiler emits a
PRINT_EXPR bytecode after every expression -- including,
apparently, ones nested within another statement.

> Doesn't it produce loads of debug-like output when pasting
> more complex things?

The fact that nobody seems to have noticed this before
(or at least not complained about it) suggests that people
don't usually paste complex things in interactive mode.

Greg




More information about the Python-list mailing list