Fale
Fale

Reputation: 147

importing module and create an instance of it

I'm currently at the beginning of learning python. I've made a game, using classes. But now I need to put these classes in another file, and importing them from within the main file. Right now I have:

a_map = Map("scene_1")
game = Engine(a_map)
game.play()

I can't seem to make an instance like this using modules. I tried:

a_map = __import__('map')
game = Engine(a_map)
game.play()

But that gives me the error

AttributeError: 'module' object has no attribute 'first_scene'

What is going wrong here? These are the Engine / Map classes:

class Engine(object):

def __init__(self, map):
    self.map = map

def play(self):
    current_scene = self.map.first_scene()
    while True: 
        next = current_scene.enter() #call return value of the current scene to 'next'
        current_scene = self.map.next_scene(next) #defines the subsequent scene 

and

class Map(object):

scenes = {"scene_1" : Scene1(),
          "scene_2" : Scene2(),
          "scene_3" : Scene3()
         }

def __init__(self, start_scene):
    self.start_scene = start_scene

#defines the first scene, using the 'scenes' array. 
def first_scene(self):
    return Map.scenes.get(self.start_scene)

#defines the second scene, using the 'scenes' array.    
def next_scene(self, next_scene):
    return Map.scenes.get(next_scene)

I'm new to programming/this website. If I gave too less/too much script information, please let me know. Thanks in advance!

Upvotes: 0

Views: 583

Answers (3)

tlrobrn
tlrobrn

Reputation: 11

Assuming your Map class is in map.py, and your Engine class is in engine.py, you just need to import them into your file. You also need to reference the module when using something defined within it. For example:

import map
import engine

a_map = map.Map('scene_1')
game = engine.Engine(a_map)
game.play()

You can also import specific items from modules, from map import Map would allow you to do a_map = Map('scene_1)

Upvotes: 0

melipone
melipone

Reputation: 36

At the beginning of each module you should list the functions/classes/modules you want to import.

If the files containing your classes are in the same directory as your main file, great, you can just do this (assuming the files containing your classes are called foo.py and bar.py):

from foo import Map
from bar import Engine

and then later on in your main file

a_map_instance = Map('scene_1')
an_engine_instance = Engine(a_map_instance)
an_engine_instance.play()

If you've got the files stored elsewhere, then you'll need to add that location to your python path. See the documentation here for how to identify the locations which are in your sys.path()

http://docs.python.org/2/tutorial/modules.html#the-module-search-path.

Upvotes: 1

bogatron
bogatron

Reputation: 19169

It appears that you are setting the map member of your Engine to the map module, not an instance of a Map object. If your Map and Engine classes are defined in map.py, then you can create your instances from the main file like this:

from map import Map, Engine
a_map = Map("scene_1")
game = Engine(a_map)
game.play()

Upvotes: 1

Related Questions