Please see my code snips below. get_simple_percentiles
and get_simple_percentile
are able to access simple_lut.__simple_lut_dict
. However, get_another_percentile
cannot access simple_lut.__another_lut
. The name of the file is simple_lut.py
.
class simple_lut(object):
__simple_lut_fname = None # filename for the LUT (Look Up Table)
__simple_lut_dict = {} # Dictionary of LUTs, one for each task
__another_lut_fname = None
__another_lut = None
def __init__(self, simple_lut_filename, another_lut_filename=None ):
# load simple LUT
# ...
for idx, curr_task in enumerate( tasks ):
# ...
self.__simple_lut_dict[ curr_task ] = tmp_dict
# load another LUT
# ...
self.__another_lut = tmp_dict
def get_simple_percentiles(self, subject):
# for each simple task, go from score to percentile
# calls get_simple_percentile
@staticmethod
def get_simple_percentile(subject, task):
# works fine here
tmp_dict = simple_lut.__simple_lut_dict[task]
@staticmethod
def get_another_percentile(subject):
# this always comes back as None!!1
tmp_dict = simple_lut.__another_lut
You only assign the attribute on the instance, not the class :
self.__another_lut = tmp_dict
The class still has the None value assigned initially. Either assign to the class or use regular methods on the instance.
simple_lut.__another_lut = tmp_dict
Assignment creates a new attribute on the instance, leaving the attribute (and value) on the class unchanged. Since the class does not see the instance attribute, it can only access the original class attribute. Modifying an attribute directly changes its value, without adding a new instance attribute on top.
Take note that your current approach (initialising the class, not the instance) is uncommon and breaks expected behavior of instances. Consider using instances without static data or not using a class at all, but a module.