0

我有一个类的实例,我在名为“Greek_gods”的模块中名为“zeus”的类中将值设置为“self.world”。我在模块名称“世界”中有另一个类名称“世界”。

我如何告诉 zeus 去“世界”并获取实例 self.world?

#module named World
class World():

    def __init__(self):
        self.world = Atlas()


#module named Greek_gods
class zeus():

    def __init__(self)
4

4 回答 4

2

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_gods
    class Zeus(object):
        def __init__(self, world):
            self.world = Atlas()
    
    world = World()
    zeus = Zeus(world)
    
  • You can make World to create Zeus:

    #module named World
    class World(object):
        def __init__(self, world):
            self.world = Atlas()
        def create_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 World
    class World(object):
        def __init__(self):
            self.world = Atlas()
    greek_world = World()
    
    
    #module named Greek_gods
    import world
    class Zeus(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).

于 2012-06-08T17:02:05.680 回答
1
# module World
class World(object):
    def __init__(self, name):
        self.name = name
        self.gods_here = set()

    def leave(self, god):
        self.gods_here.remove(god)
        return True

    def enter(self, god):
        self.gods_here.add(god)
        return True

# module Greek_Gods
class God(object):
    def __init__(self, name, world=None):
        self.name = name
        self.world = None
        self.go_to_world(world)

    def go_to_world(self, world):
        if self.world is not None:
            self.world.leave(self)
        if world is not None:
            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)
于 2012-06-08T17:00:40.020 回答
1

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

#module named World
class World():
    def __init__(self):
        self.world = Atlas()


#module named Greek_gods
from World import World

class zeus():    
    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.

于 2012-06-08T17:02:29.147 回答
0

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 World
class World():
    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_gods
import World

class zeus():
    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 
于 2012-06-08T17:02:21.007 回答