exec and locals

Chris Angelico rosuav at gmail.com
Wed Feb 26 08:40:28 EST 2014


On Thu, Feb 27, 2014 at 12:15 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> py> def spam():
> ...     exec( """x = 23""" )
> ...     return x
> ...
> py> spam()
> 23
>
>
> (My real example is more complex than this.)
>
> According to the documentation of exec, I don't think this should
> actually work, and yet it appears to.

Doesn't work for me, in IDLE in 3.4.0b2, nor in command-line Python on
3.4.0rc1+ (from hg a couple of weeks ago). But it did (happen to?)
work in 2.7. What version did you use?

ChrisA



More information about the Python-list mailing list