Timing of execution of watching functions

Hi all,

is there any sort of guarantee on when a parameter triggers functions that watch it?

E.g.

class A(param.Parameterized):
    a = param.Number()
    b = param.Number(default=0)
    
    @param.depends('a', watch=True)
    def update(self):
        self.b = 999
        
foo = A()
foo.a = 8
print(foo.b)

Always seems to print 999. Can I rely on that, or may there be a case where the foo.update function hasn’t run yet by the time I want to access the b parameter? I went into the source code and it seemed to handle asynchronous cases as well in some way, but I’m not sure which applies when.

Thanks in advance!

I can’t answer your question about asynchronicity as I’m a complete novice, but I do want to point out that there is a common case where foo.update() is not triggered when you might expect it to be, which is when you pass a to the constructor:

foo = A(a=8)
print(foo.b)
# 0

You can resolve this by setting on_init=True in @param.depends(), but this will have the side-effect of always calling foo.update() on instantiation even without setting a: on_init should only trigger if the parameter is set in the constructor · Issue #616 · holoviz/param · GitHub.

There is a pull request open to change the default behaviour so that passing parameters in the constructor always triggers watchers: Ensure that arguments to the constructor trigger watchers by philippjfr · Pull Request #620 · holoviz/param · GitHub.

Thanks for the answer! It’s not quite what I was looking for but I did some reading and found it myself now.

The docs have become really good since last time I needed to do this kind of stuff. This should be doable by specifying the precedence parameter.

1 Like