brainstate.util.copy#
- brainstate.util.copy(x, add_or_replace=None)[source]#
Copy a dictionary with optional updates.
- Overloads:
x (
FrozenDict[K,V]), add_or_replace (Mapping[K,V] |None) =None→FrozenDict[K,V]x (
dict[K,V]), add_or_replace (Mapping[K,V] |None) =None→dict[K,V]
Works with both FrozenDict and regular dict.
- Parameters:
x (
FrozenDict[K,V] |dict[K,V]) – Dictionary to copy.add_or_replace (
Mapping[K,V] |None) – Entries to add or replace in the copy.
- 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.copyCopy 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