param.script_repr#

param.script_repr(val: Parameterized, imports: list[str] | None = None, prefix: str = '\n    ', settings: list[Any] = [], qualify: bool = True, unknown_value: Any | None = None, separator: str = '\n', show_imports: bool = True) str[source]#

Generate a nearly runnable Python script representation of a Parameterized object.

The script_repr function generates a string representation of a Parameterized object, focusing on its parameter state. The output is intended to serve as a starting point for creating a Python script that, after minimal edits, can recreate an object with a similar parameter configuration. It captures only the state of the object’s parameters, not its internal (non-parameter) attributes.

Parameters:
  • val (Parameterized) – The class`Parameterized` object to be represented.

  • imports (list of str, optional) – A list of import statements to include in the output. If not provided, the function will populate this list based on the required modules.

  • prefix (str, optional) – A string prefix added to each line of the representation for indentation. Default is "\n    ".

  • settings (list of Any, optional) – A list of settings affecting the formatting of the representation. Default is an empty list.

  • qualify (bool, optional) – Whether to include fully qualified names (e.g., module.Class). Default is True.

  • unknown_value (Any, optional) – The value to use for parameters or attributes with unknown values. Default is None.

  • separator (str, optional) – The string used to separate elements in the representation. Default is "\n".

  • show_imports (bool, optional) – Whether to include import statements in the output. Default is True.

Returns:

A string representation of the object, suitable for use in a Python script.

Return type:

str

Notes

  • The output script is designed to be a good starting point for recreating the parameter state of the object. However, it may require manual edits to ensure full compatibility or to recreate complex states.

  • The imports list may not include all modules required for parameter values, focusing primarily on the modules needed for the Parameterized object itself.

References

See https://param.holoviz.org/user_guide/Serialization_and_Persistence.html#script-repr.

Examples

Create a Python script representation of a Parameterized object:

>>> import param
>>> class MyClass(param.Parameterized):
...     a = param.Number(default=10, doc="A numeric parameter.")
...     b = param.String(default="hello", doc="A string parameter.")
...
>>> instance = MyClass(a=20, b="world")
>>> print(param.script_repr(instance))
import __main__

__main__.MyClass(a=20,

        b='world')