I am having some issues with calling a function outside of a module. The scenario is I have a small class library that is using turtle to do some drawing, the function within the module calls the classes also within the module and draws things, etc. This all works fine and dandy when I call the function from within the same file, but if I have another file and call myLib.scene() I get variable undefined errors.
Code examples:
a class
class Rectangle(object):
def __init__(self, pen, height=100, width=100, fillcolor=''):
self.pen = pen
self.height = height
self.width = width
self.fillcolor = fillcolor
def draw(self, x, y):
'''draws the rectangle at coordinates x and y'''
self.pen.goto(x, y)
if self.fillcolor:
self.pen.fillcolor(self.fillcolor)
self.pen.fill(True)
self.pen.down()
for i in range(0,4):
self.pen.forward(self.height if i%2 else self.width)
self.pen.left(90)
and the calling function is this
def scene(pen):
rect = Rectangle(pen)
rect.draw(100,100)
when I put the line
scene(turtle.Turtle())
into the same file I have no issues, the rectangle is drawn and everyone goes home happy. However, if I try to call it from a separate python file like so:
myLib.scene(turtle.Turtle())
I get an error: NameError: global name 'pen' is not defined, in the for loop of my draw method. Even if the line above is in the same file it still bombs out. What is going on?