brainstate.util.copy

Contents

brainstate.util.copy#

brainstate.util.copy(x, add_or_replace=None)[source]#
Overloads:
  • x (FrozenDict[K, V]), add_or_replace (Mapping[K, V] | None) → FrozenDict[K, V]

  • x (dict[K, V]), add_or_replace (Mapping[K, V] | None) → dict[K, V]

Copy a dictionary with optional updates.

Works with both FrozenDict and regular dict.

Parameters:
Returns:

A copy of the same type as the input with updates applied.

Return type:

FrozenDict or dict

Raises:

TypeError – If x is not a FrozenDict or dict.

See also

FrozenDict.copy

Copy method for FrozenDict.

Examples

>>> from brainstate.util import FrozenDict, copy

>>> # Works with FrozenDict
>>> fd = FrozenDict({'a': 1})
>>> fd2 = copy(fd, {'b': 2})
>>> isinstance(fd2, FrozenDict)
True
>>> fd2['b']
2

>>> # Also works with regular dict
>>> d = {'a': 1}
>>> d2 = copy(d, {'b': 2})
>>> isinstance(d2, dict)
True
>>> d2['b']
2