Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- base_config = {'key1': 'val1'}
- func1(base_config + {'key2': 'val2'})
- func2(base_config + {'key3': 'val3'})
- Explicit is better than implicit.
- In the face of ambiguity, refuse the temptation to guess.
- There should be one-- and preferably only one --obvious way to do it.
- result = {'a': 1, 'b': 2} + {'a': 3, 'c': 0}
- result = {'a': 3, 'b': 2, 'c': 0}
- def dict_add_keep_last(a, b): # aka merged() or updated()
- d = a.copy()
- d.update(b)
- return d
- result = dict(a, **b) # результат тот же
- result = {**a, **b}
- result = {**b, **a} # Python 3.5, or else swap a, b: dict_add_keep_last(b, a)
- >>> from collections import Counter
- >>> Counter(a) + Counter(b)
- Counter({'a': 4, 'b': 2})
- result = {'a': [1, 3], 'b': 2, 'c': 0}
- >>> from decimal import Decimal
- >>> a = 1
- >>> a += Decimal(2)
- >>> a
- Decimal('3')
- for it in [b, c, d, ..]:
- a.update(it)
- try:
- from collections import ChainMap
- except ImportError: # Python 2
- from ConfigParser import _Chainmap as ChainMap
- import itertools
- base_config = {'key1': 'val1'}
- func1(dict(itertools.chain(base_config.iteritems(), {'key2': 'val2'}.iteritems())))
- func2(dict(itertools.chain(base_config.iteritems(), {'key3': 'val3'}.iteritems())))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement