aspis.common.assoc

aspis.common.assoc(k, v, obj)[source]

Associates a value with a key in an object.

Parameters:
  • k (Hashable) – The key to associate the value with.

  • v (Any) – The value to associate with the key.

  • obj (dict | object) – The object to associate the value with the key.

Returns:

A new object (dict or copy) with the key-value pair added/updated.

Return type:

object

Example

>>> assoc("name", "Alice", {"age": 30})
{'age': 30, 'name': 'Alice'}
>>> assoc("age", 31, {"name": "Bob", "age": 30})
{'name': 'Bob', 'age': 31}