eval or execute, is this the (most) correct way ?

Terry Reedy tjreedy at udel.edu
Mon Aug 11 17:56:21 EDT 2008



Stef Mientki wrote:
> Steven D'Aprano wrote:
>> The best solution is not to re-invent the wheel: "import code" is the 
>> way to emulate Python's interactive interpreter.
> sorry, but that confuses me even more,

"The code module provides facilities to implement read-eval-print loops 
in Python. Two classes and convenience functions are included which can 
be used to build applications which provide an interactive interpreter 
prompt."  IDLE uses it to do just that.  File idelib/pyshell.py imports 
InteractiveInterpreter.  A quarter of the file is the definition of
class ModifiedInterpreter(InteractiveInterpreter):

> I don;t have a file / module,
> just a workspace and one or more lines of code in memory.

The code module has many options. "InteractiveConsole.push(line)
Push a line of source text to the interpreter. "

Anyway, your first post indicated that those 'lines of code in memory' 
originate from console input, which, I believe, is the default input 
source for the classes.  You will need to experiment to see just how 
they work.

>> Doing a search of the file code.py, I don't find the string "eval" at 
>> all. My guess is that your approach is probably not the best way.

It would use 'exec' on statements, even if they happen to be expression 
statements.  Exec is not going away.  It is a built-in function in 3.0.

Terry Jan Reedy




More information about the Python-list mailing list