Param was designed to support nested Parameterized objects, where the value of a… Parameter is a Parameterized that has its own Parameters:
```python
import param, panel as pn
class P(param.Parameterized):
x = param.Integer(5)
y = param.ClassSelector(param.Parameterized, default=pn.widgets.IntSlider(value=7))
@param.depends("y.value", watch=True)
def fn(self):
self.x = self.y.value
p = P()
p.y.value=32
p.x
# 32
```
Here the value of `y` is a Parameterized that happens to be a [HoloViz Panel](https://panel.holoviz.org) widget that has a Parameter named `value`, and you can see that Param code can depend on that widget's Parameters.
However, several Panel core developers have noticed Panel users writing classes like:
```python
import param, panel as pn
class P(param.Parameterized):
x = param.Integer(5)
y = pn.widgets.IntSlider(value=7)
@param.depends("y.value", watch=True)
def fn(self):
self.x = self.y.value
p = P()
p.y.value=32
p.x
# 32
```
This code was not anticipated when writing Param but it happens to work in this case, because the dependency code does not actually check that `y` was defined as a Parameter before fetching the `value` parameter from that object. And the code makes sense at a semantic level, because both a `param.Integer` and a `pn.widgets.IntSlider` are objects that have an underlying integer value, so it would seem reasonable to be able to use a widget the same way one uses a Parameter.
However, because this case was never intended to be supported, a widget used as if it were a Parameter does not actually work like a Parameter in all cases. In particular, the Parameter `x` will be instantiated into an object, giving the object an independent copy of the value for `x`, while the widget `y` is a normal Python class attribute, and will thus not be instantiated. So things will seem to be working, but if the class P is ever instantiated more than once, `x` will be independent per object, and `y` will be shared across all objects, leading to untold subtle bugs.
A user can currently avoid this issue by moving the widget definition out of the class and into the constructor:
```python
import param, panel as pn
class P(param.Parameterized):
x = param.Integer(5)
def __init__(self, **params):
super().__init__(**params)
self.y = pn.widgets.IntSlider(value=7)
@param.depends("y.value", watch=True)
def fn(self):
self.x = self.y.value
p = P()
p.y.value=21
p.x
# 5 # Why is this not 32?
```
But that's a lot wordier, fails to declare the Parameter value in the class's manifest of Parameters, and in this case doesn't work (maybe because of https://github.com/holoviz/param/issues/829?).
Because people seem to fall into the trap of putting widgets at the class level quite a bit, I think we should do something about it. The options I can think of are:
1. Warn people not to do this! If we find a Parameterized as a class attribute, and certainly if we find one being depended on, warn that this attribute will not behave like a true Parameter and will not be instantiated per object, and point people to an implementation like the first one above.
2. If we find a Parameterized `q` as a class attribute of a Parameterized class, treat it as if there were a Parameter declared like `param.ClassSelector(param.Parameterized, default=q)`. We probably wouldn't actually create such a Parameter, but we'd instantiate a copy of the Parameterized into each object as if we had. I.e., just accept that people will do this, and treat it as legal. In practice I'd guess it's only Panel users who expect such usage to work, but the implementation would be for any Parameterized, and maybe there are other similar applications for this approach.
3. Under the theory that people are treating Panel Widgets and Parameters as interchangeable, one could imagine doing even more magic, so that if we find a Panel Widget (or e.g. use duck typing and find a Parameterized with a `value` Parameter), treat it as if they had actually declared that Parameter in this class (`y=param.Integer(7)` here, where `y` becomes a reference to the `value` parameter of this widget). It's hard to pin down how that could work, and it would be Panel-specific deep magic, so it would be hard to convince me that it's a good idea, but I wanted to list the idea concretely just so that we can be sure that what people seem to be assuming would work is really not something we should ever be supporting.
Personally, I vote against 3, and by default I'd vote for 1 because 2 doesn't solve a problem I have. But if 2 makes sense to other people and would support new users better, I don't think it would be difficult to implement, and I don't think it would affect code that's currently working properly. Any thoughts from others?