I have been told that I should avoid to use self.__dict__
in my code but I can't find another way around for what I am trying to do. I just begin to work with classes so not too sure how to do this.
class variable( object ):
def __init__( self, json_fn, *args, **kwargs ):
self.metrics = self.getmetrics(json_fn)
for metric in self.metrics :
self.__dict__[metric] = self.get_metric_dataframes(metric)
def get_metric_dataframes( self , metric_name ):
'''extract and return the dataframe for a metric'''
So my object "variable" has different dataframes for different metrics stored in the json. I wanted to be able to do variable.temperature, variable.pressure , variable.whatever without having to write :
self.temperature = self.get_metric_dataframes(temperature)
self.pressure = self.get_metric_dataframes(pressure)
self.whatever = self.get_metric_dataframes(whatever)
Since all the dataframes are extracted with the same function taking the metric as arguments. Which is why I looped through the metrics and use
self.__dict__[metric] = self.get_metric_dataframes(metric)
So Knowing that I will never update any of those dataframes (I use copies of them in the code but don't want to update the values of the object).
Is there another solution?
The other way i could do would be to build a dictionnary with all the metric as key and dataframe as values and store it in self.metric
, I could then call it with object.metric['temperature']
but I would rather do object.temperature
right away and I am kind of curious to know if that can be done.
Thank you for your help!