Skip to content Skip to sidebar Skip to footer

How To Import A Variable From A Different Class

I have an instance of a class that i set the value to 'self.world' inside a class named 'zeus' inside a module named 'Greek_gods'. and i have another class names 'World' inside a m

Solution 1:

You can't, because you cannot know the world instance to be added in Zeus at the moment you create Zeus.

However, you can pass an instance of World for Zeus. There are various ways to do it:

  • Pass World as a parameter to the constructor:

    #module named greek_godsclassZeus(object):def__init__(self, world):
            self.world = Atlas()
    
    world = World()
    zeus = Zeus(world)
    
  • You can make World to create Zeus:

    #module named WorldclassWorld(object):def__init__(self, world):
            self.world = Atlas()
        defcreate_zeus(self):
            zeus = Zeus()      # It would be better to pass it as a constructor 
            zeus.world = world # parameter but let us leave it simple# Using your classes:
     world = World()
     zeus = World.create_zeus()
    
  • You can create one special instance of World in the world module:

    #module named WorldclassWorld(object):def__init__(self):
            self.world = Atlas()
    greek_world = World()
    
    
    #module named Greek_gods
    import world
    classZeus(object):def__init__(self):
            self.world = world.greek_world
    

I would recommend to use the second solution; after that, the first one. The third one, however, can be very useful, too, specially if, by default, you need just one world in your application (the so-called singletons).

Solution 2:

It does exactly what you asked for - it goes to World module and takes World's instance's world attribute:

#module named WorldclassWorld():
    def__init__(self):
        self.world = Atlas()


#module named Greek_godsfrom World import World

classzeus():    
    def__init__(self)
        self.world = World().world

Although it may be done cleaner, if you could tell us what is the reason to do so. You can even shorten the code, if you assign Atlas() to the class'es property world. It will be shorter to reference it and cleaner, but will work almost the same.

Solution 3:

# module WorldclassWorld(object):
    def__init__(self, name):
        self.name = name
        self.gods_here = set()

    defleave(self, god):
        self.gods_here.remove(god)
        returnTruedefenter(self, god):
        self.gods_here.add(god)
        returnTrue# module Greek_GodsclassGod(object):
    def__init__(self, name, world=None):
        self.name = name
        self.world = None
        self.go_to_world(world)

    defgo_to_world(self, world):
        if self.world isnotNone:
            self.world.leave(self)
        if world isnotNone:
            if world.enter(self):
                self.world = world
        else:
            self.world = None

earth = World("Earth")
mars = World("Mars")

zeus = God("Zeus", mars)
zeus.go_to_world(earth)

Solution 4:

It looks like you want to use a single instance of a World class across several modules, which you can do easily by creating an instance of the World class at the top level of your World module, like this:

#module named WorldclassWorld():def__init__(self):
        self.world = Atlas()

world = World()

Now when you import World in any of your other modules, you can use World.world as the only instance of your World class. This gets a little confusing, because World.world.world is now the instance of Atlas that the World class creates, I would strongly suggest renaming something there.

Here is how your Greek_gods module might look:

#module named Greek_godsimport World

classzeus():
    world = World.world.world

Note that instead of putting world into the initializer for the zeus class, I made it a class attribute. This is because it looks like you want to share this Atlas instance (which for some reason is called world) among all zeus instances. For an example of how this would look, check out the following code (which you could put into your Greek_gods module to test):

z1 = zeus()
z2 = zeus()
assert World.world.world is z1.world is z2.world 

Post a Comment for "How To Import A Variable From A Different Class"