exec globals and locals

Chris Rebert clp2 at rebertia.com
Wed Sep 2 16:19:06 EDT 2009


On Wed, Sep 2, 2009 at 1:13 PM, Quentin Lampin<quentin.lampin at gmail.com> wrote:
> 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".

http://www.python.org/dev/peps/pep-3105/ (PEP 3105 -- Make print a function)
http://mail.python.org/pipermail/python-dev/2005-September/056154.html

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list