exec globals and locals

Chris Rebert clp2 at rebertia.com
Wed Sep 2 22:00:12 CEST 2009

On Wed, Sep 2, 2009 at 4:54 AM, Quentin Lampin<quentin.lampin at gmail.com> wrote:
> Hi,
> Being fairly new to Python, I'm trying to figure out the best way to use the
> exec statement and I must admit that I am a bit lost.
> Consider this case:
> exec "print 'a'" in {},{}   [exp.1]
> It means  that I'm (kindly) asking the interpreter to execute the code
> string  "print 'a'" with empty globals and locals.
> Considering that globals and locals are empty, I would expect [exp.1] to
> raise an exception about 'print' not being known.

In Python versions prior to 3.0, print is a statement (like for,
while, if, etc), not a function (note how you don't need parentheses
when using it); so it doesn't matter whether the built-in functions
are available or not, print will still work.


More information about the Python-list mailing list