Reference values for exec

Dave Angel davea at
Wed Oct 28 15:25:57 CET 2009

Garito wrote:
> Hi!
> I'm trying to use exec in a recursive way but I have a problem
> When I read the manual I understand that the globals and the locals are
> passed by reference but if I try to use it in a recursive way the new values
> added in a step are not passed to the next one
> Could someone point me how to do that?
> Thanks

Post some sample code, and show what doesn't work;  maybe you'll get some help that way.

I'll point out just one thing:  you cannot add new local variables at runtime.  locals() should be used to inspect, not to modify.

See the help text:
  "The contents of this dictionary should not be modified; changes may not affect 
the values of local variables used by the interpreter"

I have no idea if exec() can be safely called recursively.  Most people successfully avoid calling it at all.


More information about the Python-list mailing list