Mixin

Contents

Mixin#

class brainstate.mixin.Mixin[source]#

Base Mixin object for behavioral extensions.

The key characteristic of a Mixin is that it provides only behavioral functions without requiring initialization. Mixins are used to add specific functionality to classes through multiple inheritance without the complexity of a full base class.

Notes

Mixins should not define __init__ methods. They should only provide methods that add specific behaviors to the classes that inherit from them.

Examples

Creating a custom mixin:

>>> import brainstate
>>>
>>> class LoggingMixin(brainstate.mixin.Mixin):
...     def log(self, message):
...         print(f"[{self.__class__.__name__}] {message}")

>>> class MyComponent(brainstate.nn.Module, LoggingMixin):
...     def __init__(self):
...         super().__init__()
...
...     def process(self):
...         self.log("Processing data...")
...         return "Done"
>>>
>>> component = MyComponent()
>>> component.process()  # Prints: [MyComponent] Processing data...