exec "statement" VS. exec "statement in globals(), locals()

Duncan Booth me at privacy.net
Thu Jul 22 10:51:18 CEST 2004


roboteddy at gmail.com (Ted) wrote in 
news:9a35c34f.0407211356.4f015e9e at posting.google.com:

> According to (http://docs.python.org/ref/exec.html), "In all cases, if
> the optional parts are omitted, the code is executed in the current
> scope." Don't globals() and locals() comprise the current scope? Why
> isn't the output of each example the same?

locals() is a copy of the current scope, not the original. You should never 
expect updates to locals() to be reflected in the local variables of the 
current scope.

Are you really sure you need to use exec at all? There are very few good 
use cases where exec is the appropriate answer to a problem.



More information about the Python-list mailing list