Is this possible: scoping question with exec

Alex Martelli aleax at aleax.it
Mon Aug 27 10:00:00 EDT 2001


"Emile van Sebille" <emile at fenx.com> wrote in message
news:9m69li$kie6$1 at ID-11957.news.dfncis.de...
    ...
> As far as it being effectively the same as eval, it looks like that to me,
> but someone else will have to explain what differences there may be.  I
get
> similar results for all of:
>
> >>> exec(a.func_code, {'b':4})
> 4
> >>> eval(a.func_code, {'b':4})
> 4
> >>> exec a.func_code in {'b':4}
> 4
> >>>

You're letting the interpreter's convenient displayhook fool you.
Try instead:

>>> def a(): return b
...
>>> print repr(exec(a.func_code, {'b':4}))
  File "<stdin>", line 1
    print repr(exec(a.func_code, {'b':4}))
                  ^
SyntaxError: invalid syntax
>>> print repr(eval(a.func_code, {'b':4}))
4
>>>

and you see there are quite a few differences:-).


Alex






More information about the Python-list mailing list