Is this possible: scoping question with exec

Bengt Richter bokr at accessone.com
Fri Aug 24 20:42:35 CEST 2001


On Fri, 24 Aug 2001 07:12:57 -0700, "Emile van Sebille" <emile at fenx.com> wrote:

> exec(a.func_code, {'b':4})
>
That seems to work, but where is that form of exec statement documented?
Is that form effectively equivalent to eval?

BTW, I thought this was interesting, to see which global gets
overridden and which doesn't:

 >>> def a():
 ...     print 'a() thinks b is "%s"' % b
 ...
 >>> b='from interactive binding'
 >>> exec """
 ... print 'exec-ed string thinks a="%s",b="%s"' % (a,b)
 ... a()
 ... """ in {'a':a, 'b':'from exec dict'}
 exec-ed string thinks a="<function a at 007FA77C>",b="from exec dict"
 a() thinks b is "from interactive binding"
 >>> exec a.func_code in {'b':'from exec dict'}
 a() thinks b is "from exec dict"
 >>>




More information about the Python-list mailing list