Search code examples
pythonlistloopsclassinstance

Make operations in loop between instances in Python


I am trying to make a simple program where each instance has to make some operations in a loop:

#this is in a class 
class Sphere(Simulation):
    spheres = []
    def __init__(self, name, mass, ray, position):
        self.name = name
        self.mass = mass
        self.ray = ray
        self.position = position
        self.behaviour()

    def gravity(self):
        global G
        G = 0.0000000000667408
        gravity_force = G * self.mass / self.ray / self.ray
        return gravity_force

    def behaviour(self):
        for sphere in Sphere.spheres:
            distance_x = np.sqrt(np.square(self.position[0] - sphere.position[0]))  #need to call a planet
            distance_y = np.sqrt(np.square(self.position[1] - sphere.position[1]))
            distance_z = np.sqrt(np.square(self.position[2] - sphere.position[2]))
            distance = (distance_x, distance_y, distance_z)
            print("distance from " + self.name + str(distance))

earth = Sphere("earth", 5972000000000000000000000, 6371000, (2, 0, 0))
moon = Sphere("moon", 73420000000000000000000, 1737100, (-2, 0, 0))
mars = Sphere("mars", 639000000000000000000000, 3389500, (1, 3, 2))
earth.gravity()
moon.gravity()
mars.gravity()

The problem is: I want that each planet has to measure a distance from each other planet. So for example for the earth it will measure a distance from the moon and mars. I don't know how to do it in a loop. The distance should be a tuple of (x, y, z) values.

Thanks in advance!


Solution

  • Based on your existing code, it looks like you want to create a list of planets then use the behavior method to show the distance to other planets. For the list, you can use a class level variable then append each planet in the init method.

    Here's the working code:

    import numpy as np
    
    #this is in a class 
    class Sphere():  #Simulation):
        spheres = []   # class variable to hold all planets
        def __init__(self, name, mass, ray, position):
            self.name = name
            self.mass = mass
            self.ray = ray
            self.position = position
            #self.behaviour()
            Sphere.spheres.append(self)  # add this planet to list
    
        def gravity(self):
            global G
            G = 0.0000000000667408
            gravity_force = G * self.mass / self.ray / self.ray
            return gravity_force
    
        def behaviour(self):
            for sphere in Sphere.spheres:  # each planet in list
                if (sphere == self): continue  # skip this this planet (always 0,0,0)
                distance_x = np.sqrt(np.square(self.position[0] - sphere.position[0]))  #need to call a planet
                distance_y = np.sqrt(np.square(self.position[1] - sphere.position[1]))
                distance_z = np.sqrt(np.square(self.position[2] - sphere.position[2]))
                distance = (distance_x, distance_y, distance_z)
                print(self.name, "distance from " + sphere.name +"  " + str(distance))
    
    earth = Sphere("earth", 5972000000000000000000000, 6371000, (2, 0, 0))
    moon = Sphere("moon", 73420000000000000000000, 1737100, (-2, 0, 0))
    mars = Sphere("mars", 639000000000000000000000, 3389500, (1, 3, 2))
    earth.gravity()
    moon.gravity()
    mars.gravity()
    
    # print distances
    earth.behaviour()
    moon.behaviour()
    mars.behaviour()
    

    Output

    earth distance from moon  (4.0, 0.0, 0.0)
    earth distance from mars  (1.0, 3.0, 2.0)
    moon distance from earth  (4.0, 0.0, 0.0)
    moon distance from mars  (3.0, 3.0, 2.0)
    mars distance from earth  (1.0, 3.0, 2.0)
    mars distance from moon  (3.0, 3.0, 2.0)