Why can't you just add two dictionaries?
📁 cortege, Advanced Python Topics, example
Why can you add lists, tuples, sets, but not dictionaries?
If you don't like +, you could have made a fluent interface - so that dict.update() returns self.
The best I've found is dict(dict1, key1=value1).
Usage example:
base_config = {'key1': 'val1'}func1(base_config + {'key2': 'val2'})func2(base_config + {'key3': 'val3'})
Update: In Python 3.9, PEP 584 -- Add Union Operators To dict is implemented:
>>> {0: 'a', 1: 'b'} | {False: 'A', 2: 'C'}
{0: 'A', 1: 'b', 2: 'C'}
dictionary merging using the | (union) operation: leftmost keys (as in the | operation for set) win and rightmost (later) values win (like dict_add_keep_last() below).
If
Log in to leave an answer