How to execute lists of code? Or alternatives.

Alex cut_me_out at hotmail.com
Tue May 9 09:59:15 EDT 2000


You can probably do what you want with the exec command:

>>> exec ('print "hi"')
hi
>>> 

You might want to check out the compile command, too.  It could speed
things up a bit, if you are executing the same strings over and over:

>>> print compile.__doc__
compile(source, filename, mode) -> code object

Compile the source string (a Python module, statement or expression)
into a code object that can be executed by the exec statement or eval().
The filename will be used for run-time error messages.
The mode must be 'exec' to compile a module, 'single' to compile a
single (interactive) statement, or 'eval' to compile an expression.
>>> c = compile ('print "hi"', 'string', 'single')
>>> exec (c)
hi

Alex.



More information about the Python-list mailing list