Parameter is equal to another parameter unless a value is specified

Hey everyone,

I use most of the holoviz libraries, and I want to port a library I’m developing to Param, as it would make it integrate way better with every visualization I make (and waaay more robust, specially with the ability to have bounds).

The only thing I don’t know what to do is how to make a parameter dependent on another if I don’t say otherwise. A quick example would be this:

class B():
    def __init__(self, a=2, b=None):
        self.a = a
        self.b  = b if b else a

This would make it so the value of b is always equal to the value of a, unless I give a specific distinct value for b. So if I called obj = B(a=6) obj.b would be 6, but if I ran obj = B(b=3) obj.a would be 2 and obj.b would be 6.

Is there any easy way to implement this with the existing parameter types? I’ve tried the following

class A(param.Parameterized):
    a = param.Integer(default=4)
    b = param.Parameter(default=a)

This almost works, as I set the default value to be a. The thing is, instead of getting the same value for my b variable, I get a pointer to the parameter itself (so in this example a would be an int, while b would be an Integer, which are different types). What am I missing?

Thanks in advance!

Hi @CesarRodriguez !

This may be what you want:

class P(param.Parameterized):
    a = param.Integer(default=4)
    b = param.Parameter(default=a.default)

Alternatively you could also implement that logic in the constructor:

class P(param.Parameterized):
    a = param.Integer(default=4)
    b = param.Integer(default=None)
    def __init__(self, **params):
        self.b = self.b if self.b is not None else self.a
1 Like

That second one definitely works (the first one links b to the default of a, not whatever its value is). I just didn’t know how to merge the param object initialization with python’s native one, but I see it’s pretty simple! Thanks!