param.ParameterizedFunction#
- class param.ParameterizedFunction(*, name)[source]#
Acts like a Python function, but with arguments that are Parameters.
When a
ParameterizedFunctionis instantiated, it automatically calls its__call__method and returns the result, acting like a Python function.Features:
Declarative parameterization: Arguments are defined as
Parameterobjects, enabling type validation, bounds checking, and documentation.Dynamic updates: Changes to parameters can dynamically influence the function’s behavior.
References
See https://param.holoviz.org/user_guide/ParameterizedFunctions.html
Notes
Subclasses of
ParameterizedFunctionmust implement the__call__method, which defines the primary functionality of the function.The
instance()method can be used to obtain an object representation of the class without triggering the function’s execution.
Examples
Define a subclass of
ParameterizedFunction:>>> import param >>> class Scale(param.ParameterizedFunction): ... multiplier = param.Number(default=2, bounds=(0, 10), doc="The multiplier value.") ... ... def __call__(self, x): ... return x * self.multiplier
Instantiating the parameterized function calls it immediately:
>>> result = Scale(5) >>> result 10
Access the instance explicitly:
>>> scale3 = Scale.instance(multiplier=3) >>> scale3.multiplier 3 >>> scale3(5) 15
Customize parameters dynamically:
>>> scale3.multiplier = 4 >>> scale3(5) 20
Parameter Definitions
- __init__(**params)[source]#
Initialize a
Parameterizedinstance with optional Parameter values.Optional Parameter values must be supplied as keyword arguments (
param_name=value), overriding their default values for this one instance. Any parameters not explicitly set will retain their defined default values.If no
nameparameter is provided, the instance’snameattribute will default to an identifier string composed of the class name followed by an incremental 5-digit number.- Parameters:
**params – Optional keyword arguments mapping
Parameternames to values.- Raises:
TypeError – If one of the keywords of
paramsis not aParametername.
Examples
>>> import param >>> class MyClass(param.Parameterized): ... value = param.Number(default=10, bounds=(0, 20)) >>> obj = MyClass(value=15)
The
valueparameter is set to 15 for this instance, overriding the default.
Methods
__init__(**params)Initialize a
Parameterizedinstance with optional Parameter values.instance(**params)Create and return an instance of this class.
Attributes
param