holoviz / holoviz/param

on_init should only trigger if the parameter is set in the constructor

Open
#616 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
521
Forks
86
Avg merge
1d 13h
Merged PRs (30d)
35

Description

My hope when suggesting #513 was that a parameter would be triggered if it were passed into the constructor. It appears that the `on_init` argument always triggers on init regardless of whether the parameter was passed or not.

In the following example when `Test` is instantiated without any arguments then it still triggers the method that depends on `s`.

![image](https://user-images.githubusercontent.com/6454472/163200175-d837a155-58d8-4dbd-bb54-e47ef0c83173.png)

I think it should work equivalent to this:

```python
def __init__(self, **params):
super().__init__(**params)
if 's' in params:
self.param.trigger('s')
```

It may be useful to always trigger a method on initialization, but since that behavior is independent of any parameters then it should be a separate decorator that isn't associated with parameters. For example:

```python
class Test(param.Parameterized):
s = param.String(default='default')

@param.on_init
def run_on_initialization(self):
print(self.s)
```

Having the `on_init` argument in the `depends` decorator makes it seem like it is associated with the parameters that are also passed in to the `depends` decorator. Currently it's not, but I think it should be.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.