I'm trying to import a module, while passing some global variables, but it does not seem to work:
File test_1:
test_2 = __import__("test_2", {"testvar": 1})
File test_2:
print testvar
This seems like it should work, and print a 1, but I get the following error when I run test_1:
Traceback (most recent call last):
File ".../test_1.py", line 1, in <module>
print testvar
NameError: name 'testvar' is not defined
What am I doing wrong?
EDIT:
As I commented later on, this is an attempt to replace the functions within a graphics library. Here is a sample program (that my teacher wrote) using that library:
from graphics import *
makeGraphicsWindow(800, 600)
############################################################
# this function is called once to initialize your new world
def startWorld(world):
world.ballX = 50
world.ballY = 300
return world
############################################################
# this function is called every frame to update your world
def updateWorld(world):
world.ballX = world.ballX + 3
return world
############################################################
# this function is called every frame to draw your world
def drawWorld(world):
fillCircle(world.ballX, world.ballY, 50, "red")
############################################################
runGraphics(startWorld, updateWorld, drawWorld)
Note that this code is designed such that people who have never (or almost never) seen ANY code before (not just python) would be able to understand with little effort.
Example for rewriting the function:
Original code:
def drawPoint(x, y, color=GLI.foreground):
GLI.screen.set_at((int(x),int(y)), lookupColor(color))
Injected code:
# Where self is a window (class I created) instance.
def drawPoint(x, y, color = self.foreground):
self.surface.set_at((int(x), int(y)), lookupColor(color))
I guess my real question is: how would I inject global functions/variables into an imported module before the module runs...?