ParamDescriber#

class brainstate.mixin.ParamDescriber(cls, *desc_tuple, **desc_dict)[source]#

Parameter descriptor for deferred object instantiation.

This class stores a class reference along with arguments and keyword arguments, allowing for deferred instantiation. It’s useful for creating templates that can be reused to create multiple instances with similar configurations.

Parameters:
  • cls (TypeVar(T)) – The class to be instantiated.

  • *desc_tuple – Positional arguments to be stored and used during instantiation.

  • **desc_dict – Keyword arguments to be stored and used during instantiation.

cls#

The class that will be instantiated.

Type:

type

args#

Stored positional arguments.

Type:

tuple

kwargs#

Stored keyword arguments.

Type:

dict

identifier#

A hashable identifier for this descriptor.

Type:

tuple

Notes

ParamDescriber cannot be subclassed due to the NoSubclassMeta metaclass. This ensures consistent behavior across the codebase.

Examples

Manual creation of a descriptor:

>>> import brainstate
>>>
>>> class Network:
...     def __init__(self, n_neurons, learning_rate=0.01):
...         self.n_neurons = n_neurons
...         self.learning_rate = learning_rate
>>>
>>> # Create a descriptor
>>> network_desc = brainstate.mixin.ParamDescriber(
...     Network, n_neurons=1000, learning_rate=0.001
... )
>>>
>>> # Use the descriptor to create instances with additional args
>>> net1 = network_desc()
>>> net2 = network_desc()  # Same configuration

Using with ParamDesc mixin:

>>> class Network(brainstate.mixin.ParamDesc):
...     def __init__(self, n_neurons, learning_rate=0.01):
...         self.n_neurons = n_neurons
...         self.learning_rate = learning_rate
>>>
>>> # More concise syntax using the desc() classmethod
>>> network_desc = Network.desc(n_neurons=1000)
>>> net = network_desc(learning_rate=0.005)  # Override learning_rate
property identifier#

Get the unique identifier for this descriptor.

Returns:

A hashable identifier consisting of (class, args, kwargs).

Return type:

tuple

init(*args, **kwargs)[source]#

Alias for __call__, explicitly named for clarity.

Parameters:
  • *args – Additional positional arguments.

  • **kwargs – Additional keyword arguments.

Returns:

An instance of the described class.

Return type:

T