param.reactive.bind#
- param.reactive.bind(function, *args, watch: bool = False, **kwargs)[source]#
Bind constant values, parameters, bound functions or reactive expressions to a function.
This function creates a wrapper around the given
function, binding some or all of its arguments to constant values,Parameterobjects, or reactive expressions. The resulting function automatically reflects updates to any bound parameters or reactive expressions, ensuring that its output remains up-to-date.Similar to
functools.partial(), arguments can also be bound to constants, leaving a simple callable object. Whenwatch=True, the function is automatically evaluated whenever any bound parameter or reactive expression changes.- Parameters:
function (callable, generator, async generator, or coroutine) –
The function or coroutine to bind constant, dynamic, or reactive arguments to. It can be:
A standard callable (e.g., a regular function).
A generator function (producing iterables).
An async generator function (producing asynchronous iterables).
A coroutine function (producing awaitables).
*args (object, Parameter, bound function or reactive expression rx) – Positional arguments to bind to the function. These can be constants, param.Parameter objects, bound functions or reactive expressions.
watch (bool, optional) – If True, the function is automatically evaluated whenever a bound parameter or reactive expression changes. Defaults to False.
**kwargs (object, Parameter, bound function or reactive expression rx) – Keyword arguments to bind to the function. These can also be constants, param.Parameter objects, bound functions or reactive expressions.
- Returns:
A new function with the bound arguments, annotated with all dependencies. The function reflects changes to bound parameters or reactive expressions.
- Return type:
callable, generator, async generator, or coroutine
Examples
Bind parameters to a function:
>>> import param >>> class Example(param.Parameterized): ... a = param.Number(1) ... b = param.Number(2) >>> example = Example() >>> def add(a, b): ... return a + b >>> bound_add = param.bind(add, example.param.a, example.param.b) >>> bound_add() 3
Update a parameter and observe the updated result:
>>> example.a = 5 >>> bound_add() 7
Automatically evaluate the function when bound arguments change:
>>> bound_watch = param.bind(print, example.param.a, example.param.b, watch=True) >>> example.a = 1 # Triggers automatic evaluation 1 2