Better way of access the stateful variables objects in the given class hierarchy

I asked this question already at stackoverflow together with a serialization related part. Since the design related part receives no answers or comments I’d like to have a review on this here.

The problem: I have a complicated class hierarchy in which the classes are similar to each other but every class contains a bunch of more or less complicated stateful variables. To give you an impression, please have a look at this minimal working example:

class OptVar:     """     Complicated stateful variable     """     def __init__(self, name="", **kwargs): = name         self.parameters = kwargs   class OptVarContainer:     """     Class which contains several OptVar objects and nested OptVarContainer     classes. Is responsible for OptVar management of its sub-OptVarContainers     with their respective OptVar objects.     """     def __init__(self, name="", **kwargs): = name         for (key, value_dict) in kwargs.items():             setattr(self, key, OptVar(name=key, **value_dict))      def getAllOptVars(self):          def addOptVarToDict(var,                             dictOfOptVars={},                             idlist=[],                             reducedkeystring=""):             """             Accumulates optimizable variables in var and its linked objects.             Ignores ring-links and double links.              @param var: object to evaluate (object)             @param dictOfOptVars: optimizable variables found so far (dict of dict of objects)             @param idlist: ids of objects already evaluated (list of int)             @param reducedkeystring: to generate the dict key (string)             """              if id(var) not in idlist:                 idlist.append(id(var))                  if isinstance(var, OptVarContainer):                     for (k, v) in var.__dict__.items():                         newredkeystring = reducedkeystring + + "."                         dictOfOptVars, idlist = addOptVarToDict(v,                                                                 dictOfOptVars,                                                                 idlist,                                                                 newredkeystring)                  elif isinstance(var, OptVar):                     newreducedkeystring = reducedkeystring +                     dictOfOptVars[newreducedkeystring] = var              return dictOfOptVars, idlist          (dict_opt_vars, idlist) = addOptVarToDict(self, keystring="")         return dict_opt_vars    class C(OptVarContainer):     """     Specific implementation of class OptVarContainer     """     def __init__(self, name=""):         super(C, self).__init__(name=name,                 **{"my_c_a": {"c1": 1, "c2": 2},                    "my_c_b": {"c3": 3, "c4": 4}})   class B(OptVarContainer):     """     Specific implementation of class OptVarContainer     """     def __init__(self, name=""):         super(B, self).__init__(name=name, **{"b": {"1": 1, "2": 2}})         self.c_obj = C(name="cname")   class A(OptVarContainer):     """     Specific implementation of class OptVarContainer     """     def __init__(self, name=""):         super(A, self).__init__(name=name,                 **{"a1": {"1": 1, "2": 2},                    "a2": {"a": "a", "b": "b"}})         self.b_obj = B(name="bname")   def main():     # creating OptVarContainer with some nested OptVarContainers.     my_a_obj = A(name="aname")      # It is intended behaviour to access the OptVar objects via     # scoping within the class hierarchy.     print(my_a_obj.b_obj.b.parameters)     my_a_obj.b_obj.b.parameters["2"] = 3     print(my_a_obj.b_obj.b.parameters)     print(my_a_obj.b_obj.c_obj.my_c_a.parameters["c1"])     my_a_obj.b_obj.c_obj.my_c_a.parameters["c1"] = 6     print(my_a_obj.b_obj.c_obj.my_c_a.parameters)      # This construction is quite ugly:     # a) due to the dict: order is not fixed     # b) due to recursion (and maybe lexical sorting) it is not fast     # c) goal: hand over these optvars into some numerical optimization     # via a numpy array => should be fast     optvarsdict = my_a_obj.getAllOptVars()     print(optvarsdict)   if __name__ == "__main__":     main() 

The question is now: How to access the OptVar objects the best. I already thought about to use some kind of pool object and use some proxy within the class hierarchy to have a link to the pool. This pool should not be a singleton, since it should be possible to manage more than one pool at a time. Up to now the access to the OptVar variables within my own code is done via the getAllOptVars function. This is quite ugly and only considered temporary. Is there a better alternative to this function?

To summarize and clarify my goals:

  1. The class hierarchy is ok and reflects the model context of our problem
  2. The OptVars belong to each object in the hierarchy and should also stay there also due to model context
  3. The access and post processing of the OptVars (i.e. collecting them from the hierarchy and handing them over to an optimizer as a numpy array) is not considered to be optimal. There I need some suggestions on doing better (i.e. getting rid of isinstance and id queries).
  4. A nice to have would be: Decouple serialization of the OptVars and version management from the object hierarchy

I am aware that no unique solutions exist for this design problem, but I need further input on that.

The full context of this question is given here but after some discussions I separated the design part.