Option to make param required?

Is there a way to make an input required?

import param

class Test1(param.Parameterized):
    input_dict = param.Dict(default=None, allow_None=False)

    def __init__(self, **kwds):
        super().__init__(**kwds)

Test1() # expect this to raise an exception since no input_dict was passed

I would simply do:

import param

class Test1(param.Parameterized):
    input_dict = param.Dict(default=None, allow_None=False)

    def __init__(self, input_dict, **kwds):
        self.input_dict = input_dict
        super().__init__(**kwds)
1 Like

You can do it with a positional argument like that, but if you do, it will overwrite the value that a user may have set at the class level. In some cases setting at the class level isn’t useful or meaningful, but if it is, you can instead add an assertion or raise an exception, e.g. with if input_dict is None: raise ValueError("input_dict cannot be None"). That way a user can do Test1.input_dict=dict(a=2) ; t=Test1(), while still erroring if indeed no input_dict has been supplied at all.

1 Like