Help with list example

A change in a list doesn’t seem to trigger a depends while a number does?

import param


class try_alist(param.Parameterized):
    a_list = param.List(["a"])
    a_num = param.Number(0)

    @param.depends('a_list', watch=True)   
    def on_list_change(self):
        print(f"a_list = {self.a_list}")
        
    @param.depends('a_num', watch=True)   
    def on_num_change(self):
        print(f"a_num = {self.a_num}")

al = try_alist()

al.a_list.append("b") # no trigger

al.a_num += 1  # trigger
a_num = 1
1 Like

Hi @gbl

This is how things are expected to work. on_list_change will only trigger if you

  • trigger the a_list parameter or
  • Replace the a_list value

as shown below.

import param


class try_alist(param.Parameterized):
    a_list = param.List(["a"])
    a_num = param.Number(0)

    @param.depends("a_list", watch=True)
    def on_list_change(self):
        print(f"a_list = {self.a_list}")

    @param.depends("a_num", watch=True)
    def on_num_change(self):
        print(f"a_num = {self.a_num}")


al = try_alist()

print("No trigger because you change the list it self not the value of `a_list`")
al.a_list.append("b")  # no trigger
print("Will trigger")
al.param.trigger("a_list")  # trigger
al.a_list = ["c", "c"]  # trigger


al.a_num += 1  # trigger
a_num = 1
$ python script.py
No trigger because you change the list it self not the value of `a_list`
Will trigger
a_list = ['a', 'b']
a_list = ['c', 'c']
a_num = 1

Param can only catch when parameter values are changed. Not when a mutable value like a list, dictionary or dataframe it self changes.

2 Likes

Thanks for taking the time to reply.
That is a bit more subtle than I imagined.

Is there any writeup or documentation that delves into how param is catching these events?
Your statement above differentiating between parameter values changing and mutable values changing brings to mind how in python updating immutable variables is essentially reassigning them while mutable variables are updated in-place. So Param is tapping into reassignment?

Thanks

1 Like

Hi @gbl

There is a description of How Param Works

Perfect! Thanks.

1 Like