Find the event source with @param.depends

In a param.Parameterized class, I have several parameters which are very much alike.

idealy I would want to define a single callback method decorated with param.depends and the list of parameters. But from there there is no wayt to know which object triggered the event, unlike when using .param.watch.

I end up having to make one callback per parameter decorated with param.depends.

I tried adding self.param.watch(self.callback,[ 'param1', 'param2', 'param3']) within the __init__ method, but the event is not inputted in the callback method.
The only way is to define it outside of its definition…

Where can I find the last triggered event within a Parameterized instance?
I have noticed self.param._events, but it’s always an empty list.

The param doc is not very helpful :slight_smile:

Second that, I often have that thought.

I would also be interested in a way to batch watch params. Noticed the param.batch_watch() function, but couldn’t get it to work the way I was expecting.

Does anyone have an example for us?

I don’t to know to which extent this snippet might help you @hyamanieu, if not it may be useful for someone else:

from param import Parameterized, Parameter, depends

class A(Parameterized):
    x = Parameter(1)
    y = Parameter(2)

    def react(self, *events):
        print(f"React to events ({len(events)}):")
        for event in events:
            print(f"\tTriggered by '{event.name}' now set to {event.new} (previously equal to {event.old})")
    
    def __init__(self, **params):
        super().__init__(**params)
        watcher = self.param.watch(self.react, ["x", "y"])

a = A()

a.x = 10

a.y = 20

a.param.set_param(x=0, y=0)

a.param.trigger("y", "x")

2 Likes

Hello,

I don’t recall anymore why self.param.watch did not work in my case but your example looks like it’s working. I’ll keep it in mind, thanks!