I've got couple of huge tables with data called bnds.data and densities.data. I've also got a class processing those data tables. That class is called in a loop and in order to avoid repetitive and time demanding loading those data tables into memory, I want to creat another class and initialise some variables in there. Here's a piece of code giving the idea of what I want to do:
import numpy as np
class Pixel:
def __init__(self):
self.bounds = np.loadtxt('bnds.data')
self.rho = np.loadtxt('densities.data')
class Density(Pixel):
def __init__(self, pixel, phi, theta):
self.phi = phi
self.theta = theta
latitude = int(90 - self.phi +1)
longitude = int(180 + self.theta + 1)
n = (latitude -1)*360 + longitude -1
self.rho = pixel.bounds[n]
def print_rho(self):
print (self.rho)
pixel = Pixel()
rho = Density(pixel, 10, 20) # phi = 10, theta = 20
rho.print_rho()
Here , the instance of Pixel is sent to class Density. The data loading is done in the Pixel class. The density class will be called in a loop. What I don't quite understand is whether the Pixel class will be initialized every time the Density class is called? If yes then how to avoid it? My guess is that the Pixel class is initialized once and for all regardless of how many time the Density class is called. Is it correct? phi and theta are variables and they take different values in a loop. What I need is the bnds.data & densities.data tables to be loaded once and for all.
for i in range(90): rho = Density(pixel,i,i)the Pixel class will be referenced only once?class Density(object):