Tom
Tom

Reputation: 13

Acessing variables from one class in another class

I am stilling doing the same project as my previous question which is trying to simulate particle diffusion. My previous question does not relate to this, beyond still trying to simulate diffusion. I am at stage where I am trying to initiate different regions of diffusivity within the grid, so I have a function to produce a list of lists (coordinates) that can be checked against the coordinates of the particles at each move step to adjust the probability of movement, and as such the diffusivity.

I am using 2 classes; one to initiate the diffusion grid space, grid(), and another to move the particles, atom(). The class atom() does not inherit variables from the class grid(). A region of coordinates called t_space is created in grid() using initiate t_space and I want to access this variable t_space produced in initiate t_space in atom().

My code is very long so I have included what I hope are the relevant bits, and my current attempt at solving this.

The code I hoped would call the t_space variable from grid() in atom() was:

atom().tspace = grid().t_space

class grid():
    
    def __init__(self, x, y):
        self.grid = np.zeros((x,y))
        self.list_of_atoms=[]
        self.x = x
        self.y = y
        
    def initiate_t_space(self,t_space_x, t_space_y): 

         self.t_space = []
         for i in range(0,self.t_space_x):
             for j in range(0,self.t_space_y):
                 self.t_space.append([i,j])

 class atom():

    def __init__(self,x,y):
        self.position=[x,y]
        self.position_tracker=[]
        self.dx=0
        self.dy=0
        # self.tspace = [[11, 42], [11, 43], [11, 44], [11, 45], [11, 46], [11, 47], [11, 48], [11, 49], [11, 50], [11, 51], [11, 52], [11, 53], [11, 54], [11, 55], [11, 56], [11, 57], [11, 58], [12, 38], [12, 39], [12, 40], [12, 41], [12, 42], [12, 43], [12, 44], [12, 45], [12, 46], [12, 47], [12, 48]]
        # self.t_space = []
        # for i in range(0,100):
        #     for j in range(0,200):
        #         self.t_space.append([i,j])
        # print(self.t_space)
        atom().tspace = grid().t_space

The commented out code in the atom() class is a previous attempt to initiate t_space in atom(), however it needs to be calculated in grid() as it uses variables from grid().

Any assistance would be greatly appreciated.

Upvotes: 0

Views: 37

Answers (1)

Jeremy
Jeremy

Reputation: 671

Your variables are not class attributes but instead instance attributes. grid() instantiates your grid class, but you do not assign that instance's t_space anywhere (looks like your only way of doing that right now is to call initiate_t_space(t_space_x, t_space_y) on that created object).

If t_space was intended to be a class attribute and be the same for all grids you should create and define it as such. If it is intended to be an instance attribute you need to define it for each instance before referencing any instance's t_space anywhere.

Regardless, you need to call initiate_t_space(t_space_x, t_space_y) to set grid's t_space, which you are not doing. In other words, your program never runs the code inside your insitiate_t_space function.

Read more about classes

Upvotes: 1

Related Questions