Toolkits > com.ibm.streamsx.topology 2.1.0 > com.ibm.streamsx.topology.python > Creating SPL Operators from Python code > Python classes as SPL operators
Decorating a Python class creates a stateful SPL operator where the instance fields of the class are the operator's state. An instance of the class is created when the SPL operator invocation is initialized at SPL runtime. The instance of the Python class is private to the SPL operator and is maintained for the lifetime of the operator.
If the class has instance fields then they are the state of the operator and are private to each invocation of the operator.
If the __init__ method has parameters beyond the first self parameter then they are mapped to operator parameters. Any parameter that has a default value becomes an optional parameter to the SPL operator. Parameters of the form *args and **kwargs are not supported.
The value of the operator parameters at SPL operator invocation are passed to the __init__ method. This is equivalent to creating an instance of the class passing the operator parameters into the constructor.
For example, with this decorated class producing an SPL source operator:
@spl.source() class Range: def __init__(self, stop, start=0): self.start = start self.stop = stop def __iter__(self): return zip(range(self.start, self.stop))
The SPL operator Range has two parameters, stop is mandatory and start is optional, defaulting to zero. Thus the SPL operator may be invoked as:
// Produces the sequence of values from 0 to 99 // // Creates an instance of the Python class // Range using Range(100) // stream<int32 seq> R = Range() { param stop: 100; }
or both operator parameters can be set:
// Produces the sequence of values from 50 to 74 // // Creates an instance of the Python class // Range using Range(75, 50) // stream<int32 seq> R = Range() { param start: 50; stop: 75; }