Parameter whose value is an instance of a parameterized class

Intuitively, I would expect Parameterized to be able to be used inside other Parameterized classes like below. Why does this not work and how could I make it work, or am I abusing something here?

class T(param.Parameterized):
    a = param.Parameterized(name='foo')
    
class S(param.Parameterized):
    b = param.Number()

t = T()
t.a = 2 # should throw an error, or so I would expect
t.a = S() # should be ok... I guess

Depending on what you want to do, you could try something like this:

import param

class S(param.Parameterized):
    b = param.Number()

    def __init__(self, name, **param):
        super(S, self).__init__(**param)
        self._name = name


class T(param.Parameterized):
    a = param.ClassSelector(S, default = S('foo'))

t = T()
print(t.a._name)  # foo
#t.a = 2 # Throws an error
t.a = S('bar') # is OK

.name is already in use on Parameterized sublcasses

1 Like

Thanks @Jhsmit, that’s it. ClassSelector even appears to recognize child classes (see below).

I apologize for specifying the name, that confused my example a bit. I did in fact want to specify the name parameter as it is in use for Parameterized classes .

For completeness’s sake, without that extra complexity your example becomes

import param

class S(param.Parameterized):
    b = param.Number()

class T(param.Parameterized):
    a = param.ClassSelector(param.Parameterized)

t = T()
t.a = param.Parameterized() # is OK
t.a = S() # is OK
# t.a = 2 # Throws an error
1 Like