5

I have a module named directory and another module named species. There will always only be one directory no matter how many species i have.

In the directory module i have 1 directory class. Inside the Species module i have many different species classes.

#module named directory
class directory:

    def __init__(self):
        ...

    def add_to_world(self, obj, name, zone = 'forrest', space = [0, 0]):
        ...


#module named species
class Species (object):

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

def new(self, object_species, name, zone = 'holding'):
    self.object_species = object_species
    self.name = name
    self.zone = zone
    self.atlas.add_to_world(object_species, name)

class Lama(Species):

def __init__(self, name, atlas, zone = 'forrest'):
    self.new('Lama', name)
    self.at = getattr(Entity, )
    self.atlas = atlas

The problem is that in each of my classes i have to pass the atlas object to that species. How can i just tell the species to get an instance from a different module.

Example if i have an instance of 'atlas' in module named Entity with a class Entity, how can i tell all species with a few lines of code to grab that instance from Entity?

2
  • Your question is difficult to understand. You can import an instance just like you import anything else. If your module Entity creates an instance x, you can do import Entity.x in any other module to import that instance. Commented Jun 8, 2012 at 5:02
  • See also this question by @user1082764 -- it is courteous to cross-reference related/identical questions. Commented Jun 8, 2012 at 17:37

1 Answer 1

8

Trying to answer this question, if I understand you correctly: how do I keep a global atlas for all my species, an example of the Singleton pattern? See this SO question.

One way of doing this, simply, and Pythonically, is to have module in a file directory.py that contains all the directory-related code and a global atlas variable:

atlas = []

def add_to_world(obj, space=[0,0]):
    species = {'obj' : obj.object_species,
               'name' : obj.name,
               'zone' : obj.zone,
               'space' : space}
    atlas.append( species )

def remove_from_world(obj):
    global atlas
    atlas = [ species for species in atlas
              if species['name'] != obj.name ]

# Add here functions to manipulate the world in the directory

Then in your main script the different species could reference that global atlas by importing the directory module, thus:

import directory

class Species(object):
    def __init__(self, object_species, name, zone = 'forest'):
        self.object_species = object_species
        self.name = name
        self.zone = zone
        directory.add_to_world(self)

class Llama(Species):
    def __init__(self, name):
        super(Llama, self).__init__('Llama', name)

class Horse(Species):
    def __init__(self, name):
        super(Horse, self).__init__('Horse', name, zone = 'stable')


if __name__ == '__main__':
    a1 = Llama(name='LL1')
    print directory.atlas
    # [{'obj': 'Llama', 'name': 'LL1', 'zone': 'forest', 'space': [0, 0]}]


    a2 = Horse(name='H2')
    print directory.atlas
    # [{'obj': 'Llama', 'name': 'LL1', 'zone': 'forest', 'space': [0, 0]},
    #  {'obj': 'Horse', 'name': 'H2', 'zone': 'stable', 'space': [0, 0]}]

    directory.remove_from_world(a1)
    print directory.atlas
    # [{'obj': 'Horse', 'name': 'H2', 'zone': 'stable', 'space': [0, 0]}]

The code can be much improved, but the general principles should be clear.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.