exec globals and locals

Quentin Lampin quentin.lampin at gmail.com
Wed Sep 2 22:13:30 CEST 2009


2009/9/2 Chris Rebert <clp2 at rebertia.com>

> 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.
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>

Ok, thanks for the explanation.I'm really confused with print being a
statement but it's seems that I won't have to put too much effort on
understanding why since 3.0 states the contrary. :p
By the way, could you suggest me a link that explains why 3.0 changed this.
It might provide some material to understand the pros and cons of "function
statements".

Regards,
Quentin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090902/6cfde569/attachment.html>


More information about the Python-list mailing list