python - How to construct multi-level classes using dictionaries -


i need construction like:

mainclass["identificator 1"].categories["identificator 2"].plots["another string"].x_values 

i write many classes like:

class c_mainclass(object):      def __init__(self):         self.categories={}         self.categories["identificator 2"]=c_categories()  class c_categories(object):      def __init__(self):         self.plots={}         self.plots["another string"]=c_plots()  class c_plots(object):      def __init__(self):         self.x_values=[2,3,45,6]         self.y_values=[5,7,8,4]  mainclass={}  mainclass["identificator 1"]=c_mainclass() #mainclass["identificator bla"]=c_mainclass(bla etc)  print(mainclass["identificator 1"].categories["identificator 2"].plots["another string"].x_values) 

i'd define "subattributes" within 1 class like:

class c_mainclass={}:      setattr(mainclass["identificator 1"],"categories",{})     ...etc. 

what's practical way this?

you akin autovivification check this post out.

(this pasted answer linked question.)

class autovivification(dict):     """implementation of perl's autovivification feature."""     def __getitem__(self, item):         try:             return dict.__getitem__(self, item)         except keyerror:             value = self[item] = type(self)()             return value  = autovivification()  a[1][2][3] = 4 a[1][3][3] = 5 a[1][2]['test'] = 6  print  {1: {2: {'test': 6, 3: 4}, 3: {3: 5}}} 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -