get

Contents

get#

class brainstate.environ.get(key, default=<object object>, desc=None, *, env=None)[source]#

Get a value from the current environment.

This function retrieves values from the environment, checking first in the context stack, then in global settings. Special handling is provided for platform and device count parameters.

Parameters:
  • key (str) – The environment key to retrieve.

  • default (Any) – Default value to return if key is not found. If not provided, raises KeyError for missing keys.

  • desc (str | None) – Description of the parameter for error messages.

  • env (EnvironmentState | None) – The environment state to query. If None, uses the global environment.

Returns:

The value associated with the key.

Return type:

Any

Raises:

KeyError – If key is not found and no default is provided.

Examples

Basic retrieval:

>>> import brainstate.environ as env
>>>
>>> env.set(learning_rate=0.001)
>>> lr = env.get('learning_rate')
>>> print(f"Learning rate: {lr}")  # 0.001

With default value:

>>> import brainstate.environ as env
>>>
>>> # Get with default
>>> batch_size = env.get('batch_size', default=32)
>>> print(f"Batch size: {batch_size}")  # 32

Context-aware retrieval:

>>> import brainstate.environ as env
>>>
>>> env.set(temperature=1.0)
>>> print(env.get('temperature'))  # 1.0
>>>
>>> with env.context(temperature=0.5):
...     print(env.get('temperature'))  # 0.5
>>>
>>> print(env.get('temperature'))  # 1.0

Using custom environment:

>>> import brainstate.environ as env
>>>
>>> custom_env = env.EnvironmentState()
>>> env.set(temperature=2.0, env=custom_env)
>>> print(env.get('temperature', env=custom_env))  # 2.0

Notes

Special keys ‘platform’ and ‘host_device_count’ are handled separately and retrieve system-level information.