Clear/Remove parameters from a parameterized class

I’ve built a parameterized class that dynamically creates parameters based on the user input. Some of you may have already seen it in action here

In a few words:

  • when the user instantiate the class, he specify the parameters he needs.
  • in the __init__ method I add those parameters by using the command self.param._add_parameter("param_name-{}".format(i), v).
  • finally, those parameters are rendered on the screen with widgets.

Now to my problem: the parameters are attached to the class, not to the instance. Let’s suppose I have the following scenario:

  • the user instantiate c1 = MyClass(five_parameters) where five_parameters is a list of five parameters which are going to be added to the class. The class will be rendered on the screen and 5 widgets will be shown.
  • next, the user instantiate c2 = MyClass(two_parameters), but when the class is rendered it shows 5 widgets: the two widgets associated to two_parameters and three remaining widgets associated to the previous instance. This is a killer…

I was looking at the source of the param module and I didn’t find any “clear/remove” methods. Is there some unofficial way of removing parameters when I create a new instance?

Maybe I found a solution. At the beginning of the __init__ method I used:

        cls_name = type(self).__name__
        setattr(type(self), "_" + cls_name + "__params", dict())
        prev_params = [k for k in MyClass.__dict__.keys() 
            if "param_name-" in k]
        for p in prev_params:
            delattr(MyClass, p)

I tried a couple of examples and it seems to be working. Need more testing, though.