1

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.

  • 1
    Basically, you don't like the idea to pass the "world" instance to absolutely every other object living in that world right ? Currently I think you need to tell us a bit more about the relation between the class and the world, why do they need all a reference to this world ? – Walfrat Sep 14 '18 at 6:51
  • In addition to Walfrat's comment, could there also be a Multiverse class where WorldBeing's exist in different worlds? – Bart van Ingen Schenau Sep 14 '18 at 10:45
  • @Walfrat That's exactly the idea that I don't like. They all need a reference to the world because some have actions they might perform in the world. For example a player may have a say_hello_to_random_player method, which searches the world for a different player to say hello to. So generally the instances need to know what world they're in because they have actions they perform based on the other instances in that same world. – Pro Q Sep 14 '18 at 15:05
  • @BartvanIngenSchenau Yes! There most certainly can be. In that case, I feel like passing the parameter is normal. It's only in cases like SimpleWorld, where the class is only using one world, that it seems super repetitive to keep on passing the same parameter. – Pro Q Sep 14 '18 at 15:07
2

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.

  • About "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". How would you do that without having a reference to world in the animal? – Maxime Sep 17 '18 at 7:14
  • @Maxime have the detection logic in the world class. – Adam B Sep 17 '18 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.