I am new to programming, I have made a little program just to learn how to use python and I made this.
It works just fine if I have it in one file, but when I separate it into three files calles Atlas.py , Robot.py and Test.py.
I get an error: “Undefined Variable: Atlas” in the robot class on the init line.
I have commented which file it is inside in a comment right above.
#Atlas.py
class Atlas:
def __init__(self):
self.robots = []
self.currently_occupied = {}
def add_robot(self, robot):
self.robots.append(robot)
self.currently_occupied = {robot:[]}
#Robot.py
class Robot():
def __init__(self, rbt, atlas = Atlas): #This is the main error:"Undefined Variable: Atlas" This happens after i separate the file
self.xpos = 0
self.ypos = 0
self.atlas = atlas()
self.atlas.add_robot(rbt)
self.name = rbt
def walk(self, axis, steps=2):
....
#Test.py
robot1 = Robot("robot1")
I put these classes into the corresponding files and Test.py looks like this now:
#Test.py
import Robot
robot1 = Robot("robot1")
In Robot.py your first line should be (assuming the file is called Atlas.py):
meaning “from the Atlas.py file (also known as the Atlas module) import the Atlas class”, so the Atlas variable becomes available in the Robot.py file (also known as the Robot module).
IOW, if you need the Robot class in another file (and it still lives in the Robot module at Robot.py), you’ll need to add “from Robot import Robot” to that new file.
I suggest you read as much of the Python Tutorial as you can. Otherwise, your current problems are more directly about modules, so read that section.