André Roberge wrote:

I have a "robot" that can do some actions like "move()" and "turn_left()". I can program this robot using python like this:
====
.def move_and_turn():
. move()
. turn_left()
.
.def draw_square():
. for i in range(4):
. move_and_turn()
.
.draw_square()
========
To execute such a program within my larger program, I use
exec code in globals()


where "code" is the little program above, and it works as expected. The question I have is: how do I do this with an explicit dictionary. I would *guess* that this is somehow equivalent to "how do I create a dictionary that has access only to robot instructions [move(), turn_left(), etc.] and Python's basic syntax" ... but I don't know how to do this.

myGlobals = { 'move':move, 'turn_left':turn_left } exec code in myGlobals

You don't need to add built-ins to myGlobals. Add whatever of your symbols you want the code to have access to.

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to