Question

I'm sure there's some programming paradigm to cover this case, but I can't find the correct wording for it, and therefore my Google-fu is worthless.

I have a class called SimpleWorld that has methods, and inside those methods, the class likes to instantiate objects. Now all of these instantiated objects subclass a particular superclass that gives them a property called world. However, within my SimpleWorld, the world is always one particular value.

class WorldBeing:
    def __init__(self, world):
        self.world = world

class Animal(WorldBeing):
    pass

class Player(WorldBeing):
    pass

class SimpleWorld:
    def __init__(self):
        self.world = []

    def add_player(self):
        self.world.append(Player(self.world))

    def add_two_animals(self):
        self.world.append(Animal(self.world))
        self.world.append(Animal(self.world))

How can I structure this better, so that the self.world doesn't have to keep on being repeated for all of these animals and players?

In fact, for each WorldBeing within SimpleWorld, that being's world will always be the world of the simple world. So if I were to create another type class Plant(WorldBeing), whenever SimpleWorld instantiated a plant, that plant would also always take the same world parameter in order to be instantiated.

Can this issue be solved with a particular programming paradigm? If so, what is that paradigm called, and how would I implement it in Python?

If there's no particular paradigm, what would be the best way to simplify the SimpleWorld into not always needing to specify that parameter for each new object?

(Ideally, I'd like to assume that we can only edit SimpleWorld, but I'm willing to accept changes to the other classes as well if it really helps the structure.)

What I've considered so far:

I've considered trying to define new classes inside SimpleWorld (such as SimpleWorldAnimal), but it seems tedious to create a new internal class for each external one (and it's also difficult to define new classes that use SimpleWorld's self.world in their __init__ and still have them defined outside SimpleWorld's __init__). I'm really hoping that's not the best solution.

I've also looked into dynamic classes, but I can't think of a good way of using them yet.

Was it helpful?

Solution

I think you may be approaching your problem wrong. Why is it an object in the world’s job to know what world it’s in? It seems to me that it should be the world’s job to know what objects are inside it.

For example, an animal looking for another animal may say, “world, find me someone to talk to!” And the world would reply, “yes, there’s another animal beside you”

Alternatively. The animal may see another animal beside it, and ask the world, “is this something I can interact with”. Concretely, if an animal detected another animal somehow, it would look at the other animal, and check with the world whether they share the same world.

You could also restrict detection to only other animals within the same world. So an animal could only interact with things in its same world and never have to worry about the fact that other worlds exist. This seems like the cleanest solution.

Objects in the world should be concerned with their own functionality and only their own functionality.

Edit: just to add a bit more concrete examples.

When you instantiate a new animal...

Animal a1 = new Animal()
world1.add(a1)

Animal a2 = new Animal()
world3.add(a2)

Hope that makes it clearer.

Licensed under: CC-BY-SA with attribution
scroll top