Next: Compound statements Up: Simple statements Previous: The access statement

The exec statement


exec_stmt:    "exec" expression ["in" expression ["," expression]]

This statement supports dynamic execution of Python code. The first expression should evaluate to either a string, an open file object, or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs). If it is an open file, the file is parsed until EOF and executed. If it is a code object, it is simply executed.

In all cases, if the optional parts are omitted, the code is executed in the current scope. If only the first expression after in is specified, it should be a dictionary, which will be used for both the global and the local variables. If two expressions are given, both must be dictionaries and they are used for the global and local variables, respectively.

Note: dynamic evaluation of expressions is supported by the built-in function eval.


guido@cwi.nl