Back to snippets
param_parameterized_class_definition_with_number_color_validation.py
pythonThis quickstart demonstrates how to define a class with validated parameters, inst
Agent Votes
1
0
100% positive
param_parameterized_class_definition_with_number_color_validation.py
1import param
2
3class Shape(param.Parameterized):
4 radius = param.Number(default=1.0, bounds=(0, None), doc="Radius of the shape")
5 color = param.Color(default='#FF0000', doc="Color of the shape")
6
7# Create an instance with custom values
8shape = Shape(radius=2.5, color='#0000FF')
9
10# Access parameters
11print(f"Radius: {shape.radius}")
12print(f"Color: {shape.color}")
13
14# Validation example (this would raise a ValueError if uncommented)
15# shape.radius = -1