Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

python - Import instance of class from a different module

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...